<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/news-and-updates-surrounding-plotly-for-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/news-and-updates-surrounding-plotly-for-r/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<p>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. </p>
<p>Here’s an example of the animation capabilities supported by the Plotly package </p>

<pre>library(plotly)
library(quantmod)
library(zoo)
library(dplyr)
library(reshape2)
library(PerformanceAnalytics)

stocklist = c(&quot;AAPL&quot;,&quot;GOOGL&quot;,&quot;MSFT&quot;,&quot;BRK-A&quot;,&quot;AMZN&quot;,&quot;FB&quot;,&quot;JNJ&quot;,&quot;XOM&quot;,&quot;JPM&quot;,&quot;WFC&quot;,&quot;BABA&quot;,
              &quot;T&quot;,&quot;BAC&quot;,&quot;GE&quot;,&quot;PG&quot;,&quot;CHL&quot;,&quot;BUD&quot;,&quot;RDS-A&quot;,&quot;WMT&quot;,&quot;V&quot;,&quot;VZ&quot;,&quot;PFE&quot;,&quot;CVX&quot;,&quot;ORCL&quot;,
              &quot;KO&quot;,&quot;HD&quot;,&quot;NVS&quot;,&quot;CMCSA&quot;,&quot;DIS&quot;,&quot;MRK&quot;,&quot;PM&quot;,&quot;CSCO&quot;,&quot;TSM&quot;,&quot;C&quot;,&quot;INTC&quot;,&quot;IBM&quot;,&quot;UNH&quot;,
              &quot;HSBC&quot;,&quot;PEP&quot;,&quot;MO&quot;,&quot;UL&quot;,&quot;CX&quot;,&quot;AMGN&quot;,&quot;MA&quot;,&quot;CCV&quot;,&quot;TOT&quot;,&quot;BTI&quot;,&quot;SAP&quot;,&quot;MMM&quot;,&quot;MDT&quot;)

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

pb &lt;- txtProgressBar(min = 0, max = length(stocklist) - 1, style=3)

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

month &lt;- as.yearmon(index(ddf))
prices &lt;- data.frame(ddf, month)
names(prices) &lt;- c(stocklist, &quot;Month&quot;)
prices &lt;- melt(prices, id.vars = &quot;Month&quot;)

# Calculate returns
CalcRet &lt;- function(x, vec = F){
  ret &lt;- (x[2:length(x)] - x[1:(length(x) - 1)]) / x[1:(length(x) - 1)]
  
  if(vec == T) {
    return(ret)
  }else{
    return(mean(ret))
  }
}

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

returns &lt;- data.frame(returns, VAR = &quot;Returns&quot;)
names(returns) &lt;- c(&quot;Period&quot;, &quot;Stock&quot;, &quot;Value&quot;, &quot;Variable&quot;)

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

volatility &lt;- data.frame(volatility, VAR = &quot;Volatility&quot;)
names(volatility) &lt;- c(&quot;Period&quot;, &quot;Stock&quot;, &quot;Value&quot;, &quot;Variable&quot;)

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

p &lt;- 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 = &quot;black&quot;))) %>% 
  
  layout(title = &quot;Monthly Return vs Volatility over last 10 years &lt;br> for 50 US stocks over time&quot;,
         showlegend = F, 
         plot_bgcolor = &quot;#e6e6e6&quot;,
         paper_bgcolor = &quot;#e6e6e6&quot;) %>% 
  
  animation_opts(frame = 1000)</pre>

<p><iframe width="450" src="http://plot.ly/~RPlotBot/4358.embed"></iframe></p>
<h2> About Carson </h2>
<p><img height="300" width="300" src="https://s3.amazonaws.com/cc-user-media-prod/-KeordaLkGx83O8xLUFe%2F1489439559266-unspecified.jpg"><br>
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.</p>
<p>Follow Carson on <a href="https://twitter.com/cpsievert">Twitter</a></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/news-and-updates-surrounding-plotly-for-r/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/sk5tgf2dkpQ"></a></div>