News and Updates Surrounding plotly for R

[This article was first published on 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.

The plotly R package will soon release version 4.6.0 which includes new features that are over a year in the making. The NEWS file lists all the new features and changes. This webinar highlights the most important new features including animations and multiple linked views.

Concrete examples with code that you can run yourself will be covered in this webinar, however Carson will give you a more in depth learning experience at his workshop at plotcon taking place in Oakland on May 4th.

Here’s an example of the animation capabilities supported by the Plotly package


stocklist = c("AAPL","GOOGL","MSFT","BRK-A","AMZN","FB","JNJ","XOM","JPM","WFC","BABA",

ddf <- getSymbols(Symbols = stocklist[1], auto.assign = F)
ddf <- ddf[,6]

pb <- txtProgressBar(min = 0, max = length(stocklist) - 1, style=3)

for(i in stocklist[-1]){
  df <- getSymbols(Symbols = i, auto.assign = F)  
  df <- df[,6]
  ddf <- merge(ddf, df)
  setTxtProgressBar(pb, which(stocklist[-1] == i))

month <- as.yearmon(index(ddf))
prices <- data.frame(ddf, month)
names(prices) <- c(stocklist, "Month")
prices <- melt(prices, id.vars = "Month")

# Calculate returns
CalcRet <- function(x, vec = F){
  ret <- (x[2:length(x)] - x[1:(length(x) - 1)]) / x[1:(length(x) - 1)]
  if(vec == T) {

returns <- prices %>% 
  group_by(Month, variable) %>% 
  summarize(Return = CalcRet(value))

returns <- data.frame(returns, VAR = "Returns")
names(returns) <- c("Period", "Stock", "Value", "Variable")

# Calculate volatility
volatility <- prices %>% 
  group_by(Month, variable) %>% 
  summarize(Volatility = sd(CalcRet(value, vec = T)))

volatility <- data.frame(volatility, VAR = "Volatility")
names(volatility) <- c("Period", "Stock", "Value", "Variable")

# Create df for plotting
plot.df <- rbind(returns, volatility)
plot.df <- dcast(plot.df, Period + Stock ~ Variable, value.var = "Value")
plot.df$Year <- format(plot.df[,1], "%Y")

p <- plot_ly(plot.df, x = ~Volatility, y = ~Returns) %>% 
  add_markers(color = ~Stock, size = ~(Returns / Volatility),
              frame = ~Year,
              marker = list(opacity = 0.6, line = list(width = 1, color = "black"))) %>% 
  layout(title = "Monthly Return vs Volatility over last 10 years 
for 50 US stocks over time", showlegend = F, plot_bgcolor = "#e6e6e6", paper_bgcolor = "#e6e6e6") %>% animation_opts(frame = 1000)

About Carson

Carson Sievert is a freelance data scientist developing software and creating products that make data analysis more exciting and accessible. During his PhD, he became maintainer of the R package plotly and was recognized with the John Chambers Statistical Software Award. He is also author and maintainer of numerous other R packages including: LDAvis, animint, pitchRx, and rdom.

Follow Carson on Twitter

To leave a comment for the author, please follow the link and comment on their blog: R – Modern Data. 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.

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)