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…

1 2 |
library(magrittr) pi %>% sin == sin(pi) |

1 |
## [1] TRUE |

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

1 |
## [1] TRUE |

### Creating a pipeline

The `%>%`

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

1 2 3 4 5 6 |
# 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 |
## [1] 19.74286 |

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

1 |
## [1] 19.74286 |

### Adding visualizations to the pipeline

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

1 2 3 4 5 |
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.

1 2 3 4 5 6 7 8 9 10 11 12 |
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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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: