# Using the pipe operator in R with Plotly

**R – Modern Data**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

With the release of `Plotly 4.0`

using the `pipe %>%`

operator is a lot more intuitive when using `plot_ly()`

.

## Quick Introduction

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.

`x %>% F is the same as F(x)`

`x %>% G %>% F is the same as F(G(x))`

Here are some examples…

library(magrittr) pi %>% sin == sin(pi)

## [1] TRUE

pi %>% cos %>% sin == sin(cos(pi))

## [1] TRUE

### Creating a pipeline

The `%>%`

operator comes in handy when chaining different operations together to create a pipeline.

# 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

## [1] 19.74286

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

## [1] 19.74286

### Adding visualizations to the pipeline

The above example can be taken a step further by adding data visualization to the pipeline.

library(ggplot2) mtcars %>% subset(cyl == 6) %>% ggplot(aes(x = wt, y = mpg)) + geom_point()

## Piping and plotly

Adding plotly to a pipeline using the pipe operator is easy.

library(plotly) # Older syntax mtcars %>% subset(cyl == 6) %>% plot_ly(x = ~wt, y = ~mpg, mode = "markers", type = "scatter") # Plotly 4.0 syntax mtcars %>% subset(cyl == 6) %>% plot_ly(x = ~wt, y = ~mpg) %>% add_markers()

## Example

Using the pipe operator with dplyr verbs and plotly makes for some powerful pipelines and easy to read code.

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 = "#F35B25", symbol = "cross")) %>% add_lines(line = list(dash = "5px", width = 3, color = "#2A3356")) %>% add_text(text = ~color, textposition = "topleft", textfont = list(family = "serif", size = 20, color = "black")) %>% layout(title = "Plotly Pipeline", showlegend = F, plot_bgcolor = "#F5F5F5")

For more details visit the following resources:

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Modern Data**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.