<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/radar-charts-in-r-using-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/radar-charts-in-r-using-plotly/" data-counturl="http://www.r-bloggers.com/radar-charts-in-r-using-plotly/" data-count="horizontal" data-text="Radar 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/radar-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>This post is inspired by <a target="blank" href="http://stackoverflow.com/questions/37670412/can-i-recreate-this-polar-coordinate-graphic-in-plotly">this question on Stack Overflow.</a>. </p>
<p>We’ll show how to create excel style <b><a target="blank" href="https://en.wikipedia.org/wiki/Radar_chart">Radar Charts</a></b> in R using the plotly package.</p>

<pre>library(plotly)
library(dplyr)

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

# Melt
df &lt;- reshape2::melt(df, id = c(&quot;Company&quot;))
colnames(df) &lt;- c(&quot;Company&quot;, &quot;Complaint&quot;, &quot;Percent&quot;)

getPolarCoord &lt;- function(r, matrix = F, na = F){
  # Get starting angle and angle increments
  theta &lt;- 0
  dtheta &lt;- 360 / length(r)
  dtheta &lt;- (pi / 180) * dtheta  # in radians
  
  # Get polar coordinates
  x &lt;- c()
  y &lt;- c()
  
  for(i in 1:length(r)){
    
    x &lt;- c(x, r[i] * cos(theta))
    y &lt;- c(y, r[i] * sin(theta))
    
    theta &lt;- theta + dtheta
  }
  
  x[length(x) + 1] &lt;- x[1]
  y[length(y) + 1] &lt;- y[1]
  
  if(na == T){
    x[length(x) + 1] &lt;- NA
    y[length(y) + 1] &lt;- NA
  }
  
  
  if(matrix == T){
    return(cbind(x, y))
  }else{
    return(list(x = x, 
                y = y))
  }
  
}

coords &lt;- by(df, df[,&quot;Complaint&quot;], function(r){
  x &lt;- getPolarCoord(r[,3])
  x &lt;- cbind(x$x, x$y)
  x &lt;- data.frame(rbind(r, r[1,]), x = x[,1], y = x[,2])
  return(x)
})

coords &lt;- rbind(coords[[1]], coords[[2]], coords[[3]])
df &lt;- data.frame(coords, txt = paste(coords$Company, &quot;&lt;br>&quot;, 
                                     coords$Complaint, &quot;:&quot;, 
                                     round(coords$Percent*100, 2), &quot;%&quot;))

# Plot
smooth &lt;- 1
bgcolor &lt;- &quot;white&quot;

p &lt;- plot_ly(data = df, 
             x = x, y = y, mode = &quot;lines&quot;, 
             group = Complaint,
             fill = &quot;toself&quot;,
             line = list(smoothing = smooth, shape = &quot;spline&quot;),
             hoverinfo = &quot;text&quot;,
             text = txt) %>% 
  
  add_trace(data = df, 
            x = x, y = y, mode = &quot;markers&quot;, 
            marker = list(color = &quot;white&quot;, 
                          size = 10, 
                          line = list(width = 2)),
            hoverinfo = &quot;none&quot;,
            showlegend = F) %>% 
  
  layout(xaxis = list(title = &quot;&quot;, showgrid = F, zeroline = F, showticklabels = F,
                      domain = c(0.02, 0.48)),
         yaxis = list(title = &quot;&quot;, showgrid = F, zeroline = F, showticklabels = F,
                      domain = c(0, 0.92)),
         font = list(family = &quot;serif&quot;, size = 15),
         legend = list(x = 0.55, y = 0.9, bgcolor = &quot;transparent&quot;),
         plot_bgcolor = bgcolor,
         paper_bgcolor = bgcolor)

# Add grids
grid &lt;- rbind(getPolarCoord(rep(0.05, 50), matrix = T, na = T),
              getPolarCoord(rep(0.10, 80), matrix = T, na = T),
              getPolarCoord(rep(0.15, 150), matrix = T, na = T),
              getPolarCoord(rep(0.20, 170), matrix = T, na = T),
              getPolarCoord(rep(0.25, 200), matrix = T, na = T))

grid &lt;- as.data.frame(grid)

p &lt;- add_trace(p, data = grid,
               x = x, y = y, mode = &quot;lines&quot;,
               line = list(color = &quot;#57788e&quot;, dash = &quot;4px&quot;, width = 1),
               showlegend = F,
               hoverinfo = &quot;none&quot;)

inner &lt;- getPolarCoord(rep(0.06, 5))
outer &lt;- getPolarCoord(rep(0.27, 5))

x = t(cbind(inner$x, outer$x))
y = t(cbind(inner$y, outer$y))

x &lt;- as.numeric(apply(x, 2, function(vec){
  return(c(vec, NA))
}))

y &lt;- as.numeric(apply(y, 2, function(vec){
  return(c(vec, NA))
}))

linegrid &lt;- data.frame(x = x, y = y)

p &lt;- add_trace(p, data = linegrid,
               x = x, y = y, mode = &quot;lines&quot;,
               line = list(color = &quot;#57788e&quot;, dash = &quot;4px&quot;, width = 1),
               showlegend = F,
               hoverinfo = &quot;none&quot;)

# Add text
banks &lt;- c(&quot;Bank of&lt;br>America&quot;,
           &quot;Wells Fargo&lt;br>&amp;Company&quot;,
           &quot;JP Morgan&lt;br>Chase &amp; Co.&quot;,
           &quot;CitiBank&quot;,
           &quot;Capital One&quot;)
labels &lt;- paste0(&quot;&lt;em>&quot;, banks, &quot;&lt;/em>&quot;)
p &lt;- add_trace(p, data = getPolarCoord(rep(0.28, 5)),
               x = x, y = y, mode = &quot;text&quot;, text = labels,
               showlegend = F,
               hoverinfo = &quot;none&quot;,
               textfont = list(family = &quot;serif&quot;, color = &quot;#808080&quot;))

# Add a gray circle
p &lt;- add_trace(p, data = getPolarCoord(rep(0.24, 200)),
               x = x, y = y,
               fill = &quot;toself&quot;,
               fillcolor = &quot;rgba(200, 200, 200, 0.3)&quot;,
               line = list(color = &quot;transparent&quot;),
               mode = &quot;lines&quot;,
               hoverinfo = &quot;none&quot;,
               showlegend = F)

# Add titles, description etc

p &lt;- layout(p, 
            annotations = list(
              list(xref = &quot;paper&quot;, yref = &quot;paper&quot;, 
                   xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
                   x = 0.03, y = 1, 
                   showarrow = F, 
                   text = &quot;&lt;b>Consumer complaints for five large banks in the U.S.&lt;/b>&quot;,
                   font = list(family = &quot;serif&quot;,
                               size = 25, 
                               color = &quot;#4080bf&quot;)),
              
              list(xref = &quot;paper&quot;, yref = &quot;paper&quot;, 
                   xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
                   x = 0.03, y = 0.95, 
                   showarrow = F, 
                   text = '&lt;em>Source: Consumer Financial Protection Bureau&lt;/em>',
                   font = list(family = &quot;serif&quot;,
                               size = 16, 
                               color = &quot;#679bcb&quot;)),
              
              list(xref = &quot;paper&quot;, yref = &quot;paper&quot;, 
                   xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
                   x = 0.60, y = 0.20, 
                   showarrow = F, 
                   align = &quot;left&quot;,
                   text = &quot;Complaints received by the Consumer Financial Protection Bureau&lt;br>regarding financial products and services offered by five large banks in&lt;br>in the United States expressed as a percentage of total nummber&lt;br>of complaints.&quot;,
                   font = list(family = &quot;arial&quot;,
                               size = 12)),
              
              list(xref = &quot;paper&quot;, yref = &quot;paper&quot;, 
                   xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
                   x = 0.60, y = 0.05, 
                   showarrow = F, 
                   align = &quot;left&quot;,
                   text = '&lt;a href = &quot;https://catalog.data.gov/dataset/consumer-complaint-database&quot;>Click here to go to source&lt;/a>',
                   font = list(family = &quot;arial&quot;,
                               size = 14))
            ),
            
            shapes = list(
              list(
              xref = &quot;paper&quot;, yref = &quot;paper&quot;,
              x0 = 0, x1 = 0.95,
              y0 = 0, y1 = 1,
              type = &quot;rect&quot;,
              layer = &quot;above&quot;,
              fillcolor = &quot;rgba(191, 191, 191, 0.1)&quot;,
              line = list(color = &quot;transparent&quot;))
            ))

print(p)</pre>

<p><iframe height="600" 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/radar-charts-in-r-using-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/radar-charts-in-r-using-plotly/" data-counturl="http://www.r-bloggers.com/radar-charts-in-r-using-plotly/" data-count="vertical" data-text="Radar 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/radar-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/7skDne_7XhM"></a></div>