<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/radial-bar-charts-in-r-using-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/radial-bar-charts-in-r-using-plotly/" data-counturl="http://www.r-bloggers.com/radial-bar-charts-in-r-using-plotly/" data-count="horizontal" data-text="Radial bar charts 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-bar-charts-in-r-using-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Creating a radial barchart is fairly easy using plotly. In this post we’ll focus on modifying a radial line chart to make it look like a bar chart so come up with a nice visualization for CO<sub>2</sub> emissions. </p>
<p>The visualization is inspired by <a target="blank" href="https://s-media-cache-ak0.pinimg.com/736x/22/1a/d0/221ad079e362ba13969b1bef30b6a5f2.jpg">this awesome chart</a>. </p>

<pre># inspired by 
# https://s-media-cache-ak0.pinimg.com/736x/22/1a/d0/221ad079e362ba13969b1bef30b6a5f2.jpg

library(plotly)

# read in data
df &lt;- read.csv(&quot;https://cdn.rawgit.com/plotly/datasets/master/Emissions%20Data.csv&quot;, stringsAsFactors = F)

# Show only 2011 values
df &lt;- subset(df, Year == &quot;2011&quot;)

# Arrange in increasing order of emissions
df &lt;- df %>% dplyr::arrange(Emission)
df &lt;- df[-(1:50),]

#  Add colors
colors &lt;- RColorBrewer::brewer.pal(length(unique(df$Continent)), &quot;Spectral&quot;)
continent &lt;- unique(df$Continent)

df$colors &lt;- df$Continent

for(i in 1:length(continent)){
  idx &lt;- df$colors %in% continent[i]   
  df$colors[idx] &lt;- colors[i]
}

# Get incremental angle value
n &lt;- nrow(df) + 20
dtheta &lt;- 2*pi / n
theta &lt;- pi / 2

# Initialise
x.coord &lt;- c()
y.coord &lt;- c()
cols &lt;- c()

# This is for the white - circle in the middle
adjust &lt;-  20

# Initialize plot
p &lt;- plot_ly()

for(ctr in 1:nrow(df)){
  
  a &lt;- df$Emission[ctr] + adjust
  
  x1 &lt;- adjust * cos(theta)
  y1 &lt;- adjust * sin(theta)
  
  x2 &lt;- a * cos(theta)
  y2 &lt;- a * sin(theta)
  
  x.coord &lt;- c(x.coord, x1, x2, NA)
  y.coord &lt;- c(y.coord, y1, y2, NA)
  cols &lt;- c(cols, df$Continent[ctr], df$Continent[ctr], NA)
  
  theta &lt;- theta + dtheta
  
  p &lt;- add_trace(p, 
                 x = c(x1, x2),
                 y = c(y1, y2),
                 mode = &quot;lines&quot;, 
                 line = list(width = 5, color = df$colors[ctr]),
                 evaluate = T)
}

# Keep x and y axis extents the same
up &lt;- max(na.omit(c(x.coord, y.coord))) + 10
down &lt;- min(na.omit(c(y.coord, y.coord))) - 10

# Add layout options, shapes etc
p &lt;- layout(p,
            showlegend = F,
            xaxis = list(range = c(down, up), domain = c(0, 0.5),
                         title = &quot;&quot;, showgrid = F, zeroline = F, showticklabels = F),
            yaxis = list(range = c(down, up), 
                         title = &quot;&quot;, showgrid = F, zeroline = F, showticklabels = F),
            shapes = list(
              list(type = &quot;circle&quot;,
                   x0 = (-5 - adjust),
                   y0 = (-5 - adjust),
                   x1 = (5 + adjust),
                   y1 = (5 + adjust),
                   fillcolor = &quot;transparent&quot;,
                   line = list(color = &quot;white&quot;, width = 2)),
              
              list(type = &quot;circle&quot;,
                   x0 = (-15 - adjust),
                   y0 = (-15 - adjust),
                   x1 = (15 + adjust),
                   y1 = (15 + adjust),
                   fillcolor = &quot;transparent&quot;,
                   line = list(color = &quot;white&quot;, width = 2)),
              
              list(type = &quot;circle&quot;,
                   x0 = (-25 - adjust),
                   y0 = (-25 - adjust),
                   x1 = (25 + adjust),
                   y1 = (25 + adjust),
                   fillcolor = &quot;transparent&quot;,
                   line = list(color = &quot;white&quot;, width = 2)),
              
              list(type = &quot;circle&quot;,
                   x0 = (-35 - adjust),
                   y0 = (-35 - adjust),
                   x1 = (35 + adjust),
                   y1 = (35 + adjust),
                   fillcolor = &quot;transparent&quot;,
                   line = list(color = &quot;white&quot;, width = 2))))


# Add annotations for country names
p &lt;- plotly_build(p)

theta &lt;- pi / 2
textangle &lt;- 90

for(ctr in 1:nrow(df)){

  a &lt;- df$Emission[ctr] + adjust
  a &lt;- a + a/12

  x &lt;- a * cos(theta)
  y &lt;- a * sin(theta)
  
  if(ctr &lt; 51) {xanchor &lt;- &quot;right&quot;; yanchor &lt;- &quot;bottom&quot;}
  if(ctr > 51 &amp; ctr &lt; 84) {xanchor &lt;- &quot;right&quot;; yanchor &lt;- &quot;top&quot;}
  if(ctr > 84) {xanchor &lt;- &quot;left&quot;; yanchor &lt;- &quot;top&quot;}

  p$layout$annotations[[ctr]] &lt;- list(x = x, y = y, showarrow = F,
                             text = paste0(df$Country[ctr]),
                             textangle = textangle,
                             xanchor = xanchor,
                             yanchor = yanchor,
                             font = list(family = &quot;serif&quot;, size = 9),
                             borderpad = 0,
                             borderwidth = 0)
  theta &lt;- theta + dtheta
  textangle &lt;- textangle - (180 / pi * dtheta)
  
  if(textangle &lt; -90) textangle &lt;- 90
}

# Titles and some other details
p$layout$annotations[[148]] &lt;- list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                                    x = 0, y = 1, showarrow = F,
                                    xanxhor = &quot;left&quot;, yanchor = &quot;top&quot;,
                                    align = &quot;left&quot;,
                                    text = &quot;&lt;em>Carbon dioxide emissions&lt;/em>&lt;br>&lt;sup>(metric tons per capita)&lt;/sup>&quot;,
                                    font = list(size = 25, color = &quot;black&quot;))

p$layout$annotations[[149]] &lt;- list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                                    x = 0, y = 0.9, showarrow = F,
                                    xanxhor = &quot;left&quot;, yanchor = &quot;top&quot;,
                                    align = &quot;left&quot;,
                                    text = &quot;Emissions from burning of solid, liquid and &lt;br>gas fuels and the manufacture of cement.&quot;,
                                    font = list(size = 18, color = &quot;#808080&quot;))

p$layout$annotations[[150]] &lt;- list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                                    x = 0.15, y = 0.5, showarrow = F,
                                    xanxhor = &quot;left&quot;, yanchor = &quot;top&quot;,
                                    align = &quot;left&quot;,
                                    text = &quot;&lt;b>Annual CO&lt;sub>2&lt;/sub> emissions&lt;/b>&lt;br>&lt;b>for 147 countries.&lt;/b>&quot;,
                                    font = list(size = 13, color = &quot;black&quot;))

p$data[[149]] &lt;- list(x = rep(-7, 6), y = c(-6, -4, -2, 0, 2, 4), mode = &quot;markers&quot;,
                      marker = list(color = colors, size = 10))

p$data[[150]] &lt;- list(x = rep(1, 6), y = c(-6, -4, -2, 0, 2, 4), mode = &quot;text&quot;,
                      text = rev(continent),
                      marker = list(color = colors, size = 10))
p</pre>

<p><iframe height="1024" width="1024" src="https://plot.ly/~riddhiman/279.embed"></iframe></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="http://www.r-bloggers.com/radial-bar-charts-in-r-using-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/radial-bar-charts-in-r-using-plotly/" data-counturl="http://www.r-bloggers.com/radial-bar-charts-in-r-using-plotly/" data-count="vertical" data-text="Radial bar charts 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-bar-charts-in-r-using-plotly/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/79BqRxezoQk"></a></div>