News and Updates Surrounding plotly for R

By Riddhiman

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 <br> 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

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.