<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/using-the-pipe-operator-in-r-with-plotly/"></div></div><div><a data-url="https://www.r-bloggers.com/using-the-pipe-operator-in-r-with-plotly/" data-counturl="https://www.r-bloggers.com/using-the-pipe-operator-in-r-with-plotly/" data-count="horizontal" data-text="Using the pipe operator in R with Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/using-the-pipe-operator-in-r-with-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>With the release of <code>Plotly 4.0</code> using the <code>pipe %>%</code> operator is a lot more intuitive when using <code>plot_ly()</code>.</p>
<div>
<h2>Quick Introduction</h2>
<p>For those new to the pipe operator from the magrittr package here’s a quick introduction. In essence, the pipe operator takes the argument on the left hand side of the operator and inserts it (after evaluation if an expression) as the first argument of the expression appearing on the right hand side of the operator i.e.</p>
<p><code>x %>% F is the same as F(x)</code><br>
<code>x %>% G %>% F is the same as F(G(x))</code></p>
<p>Here are some examples…</p>

<pre>library(magrittr)
pi %>% sin  == sin(pi)</pre>

<pre>## [1] TRUE</pre>

<pre>pi %>% cos %>% sin == sin(cos(pi))</pre>

<pre>## [1] TRUE</pre>

<div>
<h3>Creating a pipeline</h3>
<p>The <code>%>%</code> operator comes in handy when chaining different operations together to create a pipeline.</p>

<pre># Manipulating and summarizing data
# Note the %$% operator exposes the data frame after subsetting 
# %>% wont work with mean since it doesn't have a data argument
mtcars %>% 
  subset(cyl == 6) %$%  # Subset based on the number of cylinders
  mean(mpg)  # Find the mean miles per gallon</pre>

<pre>## [1] 19.74286</pre>

<pre>#The above is the same as doing
mean(mtcars[mtcars$cyl == 6,]$mpg)</pre>

<pre>## [1] 19.74286</pre>

</div>
<div>
<h3>Adding visualizations to the pipeline</h3>
<p>The above example can be taken a step further by adding data visualization to the pipeline.</p>

<pre>library(ggplot2)

mtcars %>% 
  subset(cyl == 6) %>% 
  ggplot(aes(x = wt, y = mpg)) + geom_point()</pre>
<p><img height="600," width="800" src="http://i.imgur.com/lFtR8Jr.png">
</div>
</div>
<div>
<h2>Piping and plotly</h2>
<p>Adding plotly to a pipeline using the pipe operator is easy.</p>

<pre>library(plotly)

# Older syntax
mtcars %>% 
  subset(cyl == 6) %>% 
  plot_ly(x = ~wt, y = ~mpg, mode = &quot;markers&quot;, type = &quot;scatter&quot;)

# Plotly 4.0 syntax
mtcars %>% 
  subset(cyl == 6) %>% 
  plot_ly(x = ~wt, y = ~mpg) %>% 
  add_markers()</pre>
<p><iframe height="600" width="800" src="https://plot.ly/~riddhiman/393.embed"><br>
</iframe>
</div>
<div>
<h2>Example</h2>
<p>Using the pipe operator with dplyr verbs and plotly makes for some powerful pipelines and easy to read code.</p>

<pre>library(dplyr)
library(plotly)

diamonds %>% 
  group_by(color) %>% 
  summarize(Avg.Price = mean(price),
            Avg.Carat = mean(carat),
            Min.Price = min(price),
            Max.Price = max(price)) %>% 
  plot_ly(y = ~Avg.Price, x = ~Avg.Carat) %>% 
  add_markers(marker = list(size = 12, color = &quot;#F35B25&quot;, symbol = &quot;cross&quot;)) %>% 
  add_lines(line = list(dash = &quot;5px&quot;, width = 3, color = &quot;#2A3356&quot;)) %>% 
  add_text(text = ~color, textposition = &quot;topleft&quot;,
           textfont = list(family = &quot;serif&quot;, size = 20, color = &quot;black&quot;)) %>% 
  layout(title = &quot;Plotly Pipeline&quot;, showlegend = F,
         plot_bgcolor = &quot;#F5F5F5&quot;)</pre>
<p><iframe height="600" width="800" src="https://plot.ly/~riddhiman/395.embed"><br>
</iframe>
</div>
<p>For more details visit the following resources:</p>
<ul>
<li><a target="_blank" href="https://github.com/smbache/magrittr">Magrittr vignette on Github</a></li>
<li><a target="_blank" href="https://cpsievert.github.io/plotly_book">Carson’s interactive book on Plotly for R</a></li>
</ul>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/using-the-pipe-operator-in-r-with-plotly/"></div></div><div><a data-url="https://www.r-bloggers.com/using-the-pipe-operator-in-r-with-plotly/" data-counturl="https://www.r-bloggers.com/using-the-pipe-operator-in-r-with-plotly/" data-count="vertical" data-text="Using the pipe operator in R with Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/using-the-pipe-operator-in-r-with-plotly/"> R – Modern Data</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/N2N0HM9qDgM"></a></div>