<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/pipe-dream/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dariustg.github.io/2016/12/pipe-dream.html"> R-SquareD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> Plusses and Arrows and Percents, oh my!. – </p>
<p>Do you continually substitute “%>%” for “+” when switching between data wrangling and data visualization? I’ve got just the solution for you!</p>
<p><img title="Pipe Dream" alt="Pipes" src="https://i2.wp.com/dariustg.github.io//img/posts/pipes.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Count myself as one of those people that continually use a pipe instead of a plus and vice-verca when I’m writing a lot of code. Sir Hadley has basically shit the door on ever switching ggplot to using magrittr pipes and I don’t blame him. But he can’t stop me from doing whatever the heck I want.</p>
<p>In the following code, I took the sample ggplot code from the help and modified it to use magrittr.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>magrittr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>geom_point_p</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>p</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span>return</span><span>(</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>...</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>geom_errorbar_p</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>p</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span>return</span><span>(</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>geom_errorbar</span><span>(</span><span>...</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>df</span><span> </span><span>=</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>rep</span><span>(</span><span>letters</span><span>[</span><span>1</span><span>:</span><span>3</span><span>],</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>10</span><span>)),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>30</span><span>)</span><span>
</span><span>)</span><span>
</span><span>ds</span><span> </span><span>=</span><span> </span><span>plyr</span><span>::</span><span>ddply</span><span>(</span><span>df</span><span>,</span><span> </span><span>&quot;gp&quot;</span><span>,</span><span> </span><span>plyr</span><span>::</span><span>summarise</span><span>,</span><span> </span><span>mean</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>y</span><span>),</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>sd</span><span>(</span><span>y</span><span>))</span><span>
</span><span># The summary data frame ds is used to plot larger red points on top
# of the raw data. Note that we don't need to supply `data` or `mapping`
# in each layer because the defaults from ggplot() are used.
</span><span>ggplot</span><span>(</span><span>df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>gp</span><span>,</span><span> </span><span>y</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>geom_point_p</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>geom_point_p</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>ds</span><span>,</span><span> </span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>mean</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>'red'</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-1" src="https://i2.wp.com/dariustg.github.io/figure/source/2016-12-07-pipe-dream/unnamed-chunk-1-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<figure>
<pre><code data-lang="r"><span># Same plot as above, declaring only the data frame in ggplot().
# Note how the x and y aesthetics must now be declared in
# each geom_point() layer.
</span><span>ggplot</span><span>(</span><span>df</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>geom_point_p</span><span>(</span><span>aes</span><span>(</span><span>gp</span><span>,</span><span> </span><span>y</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>geom_point_p</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>ds</span><span>,</span><span> </span><span>aes</span><span>(</span><span>gp</span><span>,</span><span> </span><span>mean</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>'red'</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-1" src="https://i1.wp.com/dariustg.github.io/figure/source/2016-12-07-pipe-dream/unnamed-chunk-1-2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<figure>
<pre><code data-lang="r"><span># Alternatively we can fully specify the plot in each layer. This
# is not useful here, but can be more clear when working with complex
# mult-dataset graphics
</span><span>ggplot</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>geom_point_p</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>gp</span><span>,</span><span> </span><span>y</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>geom_point_p</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>ds</span><span>,</span><span> </span><span>aes</span><span>(</span><span>gp</span><span>,</span><span> </span><span>mean</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>'red'</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>geom_errorbar_p</span><span>(</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>ds</span><span>,</span><span>
</span><span>aes</span><span>(</span><span>gp</span><span>,</span><span> </span><span>mean</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>mean</span><span> </span><span>-</span><span> </span><span>sd</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>mean</span><span> </span><span>+</span><span> </span><span>sd</span><span>),</span><span>
</span><span>colour</span><span> </span><span>=</span><span> </span><span>'red'</span><span>,</span><span>
</span><span>width</span><span> </span><span>=</span><span> </span><span>0.4</span><span>
</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-1" src="https://i1.wp.com/dariustg.github.io/figure/source/2016-12-07-pipe-dream/unnamed-chunk-1-3.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I may have to roll this into a package at some point.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://dariustg.github.io/2016/12/pipe-dream.html"> R-SquareD</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/rX5klzdtIHo"></a></div>