<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/the-grammar-of-graphics-and-radar-charts/"></div></div><div><a data-url="https://www.r-bloggers.com/the-grammar-of-graphics-and-radar-charts/" data-counturl="https://www.r-bloggers.com/the-grammar-of-graphics-and-radar-charts/" data-count="horizontal" data-text="The Grammar of Graphics and Radar Charts" 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://www.r-chart.com/2016/10/the-grammar-of-graphics-and-radar-charts.html"> R-Chart</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<div><span>A radar chart (also known as a web chart, spider chart, or star chart) is described on <a target="_blank" href="https://en.wikipedia.org/wiki/Radar_chart">Wikipedia</a> as ‘a two-dimensional chart of three or more quantitative variables represented on axes starting from the same point.’  </span></div>
<div><span><br></span></div>
<div><a target="_blank" href="https://i2.wp.com/2.bp.blogspot.com/-MwCucP8iX-A/WAJJF7vSj2I/AAAAAAAAAzg/P9N4U4gEMag2ml5NvGfxCvn_sYDzbcBJACEw/s1600/polar_finished.png?ssl=1"><img src="https://i0.wp.com/2.bp.blogspot.com/-MwCucP8iX-A/WAJJF7vSj2I/AAAAAAAAAzg/P9N4U4gEMag2ml5NvGfxCvn_sYDzbcBJACEw/s640/polar_finished.png?resize=456%2C224&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<div><span>Although this is an accurate description, it does not express the design and structure of a chart in a way that relates it to other types of charts.  The grammar of graphics embodied by ggplot2 provides not only a way of representing such a chart, but also utilizes a syntax that can help one compare it to other types of charts.  A radar chart might be described using ggplot2 terminology as being <b><i>a line chart with a completed path plotted using a polar rather than a cartesian coordinate system.</i></b></span></div>
<p><span><br></span><span>The components of this definition map to ggplot functions:</span></p>
<ul>
<li><span>The phrase “a line chart” suggests the geom_line() function.  We can do our first exploratory plots using a line chart and modify the results incrementally.</span></li>
<li><span>The phrase “with a completed path” indicates that a line is not sufficient.  We need to ensure the beginning and end meet.  The geom_polygon() function can be used to generate the outline of a polygon to accomplish this.</span></li>
<li><span>The final requirement is that the chart be “plotted using a polar rather than a cartesian coordinate system”.  The coord_polar() function accomplishes this transformation.  </span></li>
</ul>
<p><span></span><br><span>Throughout this post, the equals assignment operator will be used instead of “less-than-dash” because blogger mangles this character sequence.  I prefer the latter stylistically.  The code in this post is available in a <a target="_blank" href="https://github.com/ezgraphs/R-Programs/blob/master/radar_charts.R">script at GitHub</a> as well.  Start by importing the following packages.</span><br><span><br></span><span><b>library(dplyr)</b></span></div>
<div><span><b>library(ggplot2)</b></span></div>
<div><span><b>library(scales)</b></span></div>
<div><span><b>library(reshape2)</b></span></div>
<div><span><b>library(tibble)</b></span></div>
<div><span><br></span><span>Dplyr and reshape are used to structure and filter the dataset.  The scales package is used to normalize data values for convenient comparison.  The tibble package can be imported to provide convenient viewing and utility functions when working through dplyr pipelines, and ggplot2 is used to create the charts.</span>
<div></div>
<p><span>Create a data frame based on the mtcarts dataset. The rownames_to_column function from the tibble package is used to create a new column named “cart” based on the rownames.  The rescale function from the scales package transforms all numeric variables in the dataset so that they have comparable values (between 0 and 1).  The melt function from the reshape package creates name and value columns and populates them with the names and values that previously were independent columns.  The data set is now “longer” and not tidy, but the format is useful for the plots created later.  The data set is then ordered by the name of the car.</span></div>
<div></div>
<div>
<div><b>df = mtcars %>%</b></div>
<div><b> rownames_to_column( var = “car” ) %>% </b></div>
<div><b> mutate_each(funs(rescale), -car) %>% </b></div>
<div><b> melt(id.vars=c(‘car’), measure.vars=colnames(mtcars)) %>% </b></div>
<div><b> arrange(car)</b></div>
<p></div>
<div><span>A radar chart is really just a line plot altered to be charted in an alternative coordinate system. </span>
<div></div>
</div>
<div><b>line_plot = df %>%</b></div>
<div><b> filter(variable==’mpg’) %>%</b></div>
<div><b> ggplot(aes(x=car, y=value, group=1)) + </b></div>
<div><b> geom_line(color = ‘purple’)</b></div>
<div><b><br></b></div>
<div>
<div><span><b>print(line_plot)</b></span></div>
<div><span><br></span></div>
<div><a target="_blank" href="https://i1.wp.com/3.bp.blogspot.com/-F8J0gCIzuOs/WAIYGbUQhNI/AAAAAAAAAyc/He6KYbSu36wJItKjAF6JfXr_v_jBuihPwCLcB/s1600/line_plot.png?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-F8J0gCIzuOs/WAIYGbUQhNI/AAAAAAAAAyc/He6KYbSu36wJItKjAF6JfXr_v_jBuihPwCLcB/s640/line_plot.png?resize=456%2C223&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span>Compare the line plot above with the same plot modified to use polar coordinates.</span>
<div><span><br></span><span><b>print(line_plot + coord_polar())</b></span></div>
<div><a target="_blank" href="https://i0.wp.com/1.bp.blogspot.com/-zJXo5bWFHgM/WAIYMWIHP9I/AAAAAAAAAyg/FFrQLtstTa0qKd67QIwTb66-ah324zH-wCLcB/s1600/line_plot_polar.png?ssl=1"><img src="https://i1.wp.com/1.bp.blogspot.com/-zJXo5bWFHgM/WAIYMWIHP9I/AAAAAAAAAyg/FFrQLtstTa0qKd67QIwTb66-ah324zH-wCLcB/s640/line_plot_polar.png?resize=456%2C223&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div><span><br></span><span><br></span></div>
</div>
<div><span>The result is not exactly a radar chart, there is a gap where the beginning and the end of the line were not connected.  We need to connect the line so that there is a completed path with no beginning and endpoint.  This can be accomplished using the geom_polygon instead of geom_line function.  Since we only want an outline (not a filled polygon) we specify fill=NA as an argument.  This looks a bit funny when plotted using a standard cartesian coordinate system.</span>
<div></div>
</div>
<div><b>polygon_plot = df %>% </b></div>
<div><b> filter(variable==’mpg’) %>%</b></div>
<div><b> ggplot(aes(x=car, y=value, group=1)) + </b></div>
<div><b> geom_polygon(color = ‘purple’, fill=NA)</b></div>
<div>
<div><b><br></b></div>
<div><b>print(polygon_plot)</b></div>
<div></div>
<div><a target="_blank" href="https://i1.wp.com/2.bp.blogspot.com/-ctmg5XTtQ3E/WAJFZaVlQyI/AAAAAAAAAy4/B-hoCWPvfUYASShn8vifwQhPSyLBG1IlgCLcB/s1600/polygon_plot.png?ssl=1"><img src="https://i1.wp.com/2.bp.blogspot.com/-ctmg5XTtQ3E/WAJFZaVlQyI/AAAAAAAAAy4/B-hoCWPvfUYASShn8vifwQhPSyLBG1IlgCLcB/s640/polygon_plot.png?resize=456%2C224&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<div><span>The result rendered with polar coordinates now includes a completed path.</span></div>
<div><span><br></span></div>
<div><span><b>print(polygon_plot + coord_polar())</b></span></div>
</div>
<div>
<div></div>
<div><a target="_blank" href="https://i2.wp.com/4.bp.blogspot.com/-bzdgWKQoboM/WAJFy_RYHEI/AAAAAAAAAy8/6iGI0Fq9R0MttvLzFeYthPnBCO2w2QzegCLcB/s1600/poly_polar.png?ssl=1"><img src="https://i2.wp.com/4.bp.blogspot.com/-bzdgWKQoboM/WAJFy_RYHEI/AAAAAAAAAy8/6iGI0Fq9R0MttvLzFeYthPnBCO2w2QzegCLcB/s640/poly_polar.png?resize=456%2C224&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<div></div>
<p><span>A bit of cleanup of the themes and label orientation can improve the presentation further.</span></div>
<div>
<div></div>
<div><span><b>print(polygon_plot + coord_polar() + </b></span></div>
<div><span><b>        theme_bw() + </b></span></div>
<div><span><b>        theme(axis.text.x = </b></span></div>
<div><span><b>    element_text(</b></span></div>
<div><span><b>      vjust=50,</b></span></div>
<div><span><b>      angle=-90 – 360 / length(unique(df$car)) * seq_along(df$car)</b></span></div>
<div><span><b>    )</b></span></div>
<div><span><b>        )</b></span></div>
<div><span><b>)</b></span></div>
<div><span><br></span></div>
<div></div>
<div><a target="_blank" href="https://i2.wp.com/1.bp.blogspot.com/-w5BOY3MvkA0/WAJJUrUcw3I/AAAAAAAAAzc/ea5oiOnqXAg_8I9erWAEX4B2XrmeZy7jgCLcB/s1600/polar_final.png?ssl=1"><img src="https://i2.wp.com/1.bp.blogspot.com/-w5BOY3MvkA0/WAJJUrUcw3I/AAAAAAAAAzc/ea5oiOnqXAg_8I9erWAEX4B2XrmeZy7jgCLcB/s640/polar_final.png?resize=456%2C224&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<div></div>
<p><span>One of the values of radar charts is that the result has a distinctive “shape” that can help to highlight certain patterns or similarities between results.  Multifaceted charts can be used to quickly render a set of radar charts.  This example plots all car data variables and facets by variable type.</span></div>
<div><b><br></b></div>
<div><b>df %>%</b></div>
<div><b> ggplot(aes(x=car, y=value, group=variable, color=variable)) + </b></div>
<div><b> geom_polygon(fill=NA) + </b></div>
<div><b> coord_polar() + theme_bw() + facet_wrap(~ variable) + </b></div>
<div><b> #scale_x_discrete(labels = abbreviate) + </b></div>
<div><b> theme(axis.text.x = element_text(size = 3))</b></div>
<div>
<div></div>
<div><a target="_blank" href="https://i0.wp.com/4.bp.blogspot.com/-QjvYsb9ouqQ/WAJKSdpBsAI/AAAAAAAAAzk/07szfsSx1qwUv2M-sJ8EO16ncbUWZ9__ACLcB/s1600/Rplot.png?ssl=1"><img src="https://i1.wp.com/4.bp.blogspot.com/-QjvYsb9ouqQ/WAJKSdpBsAI/AAAAAAAAAzk/07szfsSx1qwUv2M-sJ8EO16ncbUWZ9__ACLcB/s640/Rplot.png?resize=456%2C224&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<div></div>
<p><span>This final example facets by car rather than variable type.  Cars with related characteristics share a similar shape.</span></div>
<div></div>
<div><b>df %>%</b></div>
<div><b> ggplot(aes(x=variable, y=value, group=car, color=car)) + </b></div>
<div><b> geom_polygon(fill=NA) + </b></div>
<div>
<div><b> coord_polar() + theme_bw() + facet_wrap(~ car)</b> </div>
<div></div>
<div><a target="_blank" href="https://i1.wp.com/3.bp.blogspot.com/-eZME_fJaNvQ/WAJKstoHNWI/AAAAAAAAAzs/TuIbJmf4A4cXXUPAb1pfToGuL80CrCXvgCLcB/s1600/polar_by_car.png?ssl=1"><img src="https://i0.wp.com/3.bp.blogspot.com/-eZME_fJaNvQ/WAJKstoHNWI/AAAAAAAAAzs/TuIbJmf4A4cXXUPAb1pfToGuL80CrCXvgCLcB/s640/polar_by_car.png?resize=456%2C224&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<p><span>Ggplot2 not only renders good-looking charts… it enables you to reason about them based upon a well thought out API.  Although radar plots are not currently included by default in ggplot2, if you reason about their structure a bit, you can “build up” a chart from the functions available.  <a target="_blank" href="http://www.r-chart.com/2010/07/pie-charts-in-ggplot2.html">Pie charts</a> are also not included in ggplot2 but can be constructed using a stacked bar chart and specifying an appropriate coordinate system.  Ggplot2 provides a playground where you can train yourself to better understand the components of charts and how various chart types relates.  Working with the package in this way will help you see that the “gg” in ggplot2 is as significant as the plotting itself.</span>
<div></div>
</div>
<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/the-grammar-of-graphics-and-radar-charts/"></div></div><div><a data-url="https://www.r-bloggers.com/the-grammar-of-graphics-and-radar-charts/" data-counturl="https://www.r-bloggers.com/the-grammar-of-graphics-and-radar-charts/" data-count="vertical" data-text="The Grammar of Graphics and Radar Charts" 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://www.r-chart.com/2016/10/the-grammar-of-graphics-and-radar-charts.html"> R-Chart</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/t7O_q_gqps8"></a></div>