Using the pipe operator in R with Plotly

October 18, 2016
By

(This article was first published on R – Modern Data, and kindly contributed to R-bloggers)

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:

To 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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)