<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/radial-stacked-area-chart-in-r-using-plotly/"></div></div><div><a data-url="https://www.r-bloggers.com/radial-stacked-area-chart-in-r-using-plotly/" data-counturl="https://www.r-bloggers.com/radial-stacked-area-chart-in-r-using-plotly/" data-count="horizontal" data-text="Radial Stacked Area Chart in R using Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/radial-stacked-area-chart-in-r-using-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post we’ll quickly show how to create radial stacked ara charts in plotly. We’ll use the <code>AirPassengers</code> dataset. </p>
<p>Inspired by Mike Bostocks post: <a target="_blank" href="http://bl.ocks.org/mbostock/3048740">http://bl.ocks.org/mbostock/3048740</a></p>

<pre>#devtools::install_github(&quot;ropensci/plotly&quot;)

library(plotly)
library(zoo)
library(data.table)

# Load Airpassengers data set
data(&quot;AirPassengers&quot;)

# Create data frame with year and month
AirPassengers &lt;- zoo(coredata(AirPassengers), order.by = as.yearmon(index(AirPassengers)))
df &lt;- data.frame(month = format(index(AirPassengers), &quot;%b&quot;),
                 year =  format(index(AirPassengers), &quot;%Y&quot;),
                 value = coredata(AirPassengers))

# Get coordinates for plotting
#Angles for each month
nMonths &lt;- length(unique(df$month))
theta &lt;- seq(0, 2*pi, by = (2*pi)/nMonths)[-(nMonths+1)]

# Append these angles to the data frame
df$theta &lt;- rep(theta, nMonths)

# Cumulatively sum number of passgengers
dt &lt;- as.data.table(df)
dt[,cumvalue := cumsum(value), by = month]
df &lt;- as.data.frame(dt)

# Cartesian coordinates (x, y) space will be value*cos(theta) and value*sin(theta)
df$x &lt;- df$cumvalue * cos(df$theta)
df$y &lt;- df$cumvalue * sin(df$theta)

# Create hovertext
df$hovertext &lt;- paste(&quot;Year:&quot;, df$year, &quot;&lt;br>&quot;,
                      &quot;Month:&quot;, df$month, &quot;&lt;br>&quot;,
                      &quot;Passegers:&quot;, df$value)

# Repeat January values
ddf &lt;- data.frame()
for(i in unique(df$year)){
  temp &lt;- subset(df, year == i)
  temp &lt;- rbind(temp, temp[1,])
  ddf &lt;- rbind(ddf, temp)
}

df &lt;- ddf

# Plot
colorramp &lt;- colorRampPalette(c(&quot;#bfbfbf&quot;, &quot;#f2f2f2&quot;))
cols &lt;- colorramp(12)

cols &lt;- rep(c(&quot;#e6e6e6&quot;, &quot;#f2f2f2&quot;), 6)

linecolor &lt;- &quot;#737373&quot;

p &lt;- plot_ly(subset(df, year == 1949), x = ~x, y = ~y, hoverinfo = &quot;text&quot;, text = ~hovertext,
             type = &quot;scatter&quot;, mode = &quot;lines&quot;,
             line = list(shape = &quot;spline&quot;, color = linecolor))

k &lt;- 2
for(i in unique(df$year)[-1]){
  p &lt;- add_trace(p, data = subset(df, year == i), 
                 x = ~x, y = ~y, hoverinfo = &quot;text&quot;, text = ~hovertext,
                 type = &quot;scatter&quot;, mode = &quot;lines&quot;,
                 line = list(shape = &quot;spline&quot;, color = linecolor),
                 fillcolor = cols[k], fill = &quot;tonexty&quot;)
  
  k &lt;- k + 1
}

start &lt;- 100
end &lt;- 4350
axisdf &lt;- data.frame(x = start*cos(theta), y = start*sin(theta),
                     xend = end*cos(theta), yend = end*sin(theta))

p &lt;- add_segments(p = p, data = axisdf, x = ~x, y = ~y, xend = ~xend, yend = ~yend, inherit = F,
                  line = list(dash = &quot;8px&quot;, color = &quot;#737373&quot;, width = 4),
                  opacity = 0.7)

p &lt;- add_text(p, x = (end + 200)*cos(theta), y = (end + 200)*sin(theta), text = unique(df$month), inherit = F,
              textfont = list(color = &quot;black&quot;, size = 18))

p &lt;- layout(p, showlegend = F,
       title = &quot;Radial Stacked Area Chart&quot;,
       xaxis = list(showgrid = F, zeroline = F, showticklabels = F, domain = c(0.25, 0.80)),
       yaxis = list(showgrid = F, zeroline = F, showticklabels = F),
       length = 1024,
       height = 600)

p</pre>
<p><iframe height="800" width="1024" src="https://plot.ly/~riddhiman/389.embed"></iframe>Radial Stacked Area Chart</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/radial-stacked-area-chart-in-r-using-plotly/"></div></div><div><a data-url="https://www.r-bloggers.com/radial-stacked-area-chart-in-r-using-plotly/" data-counturl="https://www.r-bloggers.com/radial-stacked-area-chart-in-r-using-plotly/" data-count="vertical" data-text="Radial Stacked Area Chart in R using Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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/radial-stacked-area-chart-in-r-using-plotly/"> 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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/IEtNijdzF_A"></a></div>