<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/extreme-pie-chart-polishing/"></div></div><div><a data-url="https://www.r-bloggers.com/extreme-pie-chart-polishing/" data-counturl="https://www.r-bloggers.com/extreme-pie-chart-polishing/" data-count="horizontal" data-text="Extreme pie chart polishing" 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://ellisp.github.io/blog/2016/11/15/piecharts"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The usual response from statisticians and data professionals to pie charts ranges from lofty disdain to outright snobbery. But sometimes I think they’re the right tool for communication with a particular audience. Like others I was struck by <a target="_blank" rel="nofollow" href="http://www.stuff.co.nz/national/86458731/Cheviot-earthquake-Tracing-the-source-of-the-7-5-magnitude-quake-and-its-aftermath">this image from New Zealand news site stuff.co.nz</a> showing that nearly half the earthquake energy of the past six years came in one day (last Sunday night, and the shaking continues by the way). Pie charts work well when the main impression of relative proportions to the whole is obvious, and fine comparisons aren’t needed.</p>
<p>Here’s my own version of the graphic. I polished this up during a break while working at home due to the office being shut for earthquake-related reasons:</p>
<p><img alt="pie" src="http://ellisp.github.io/img/0067-pie.svg"></p>
<p>Some of the particular aspects of pie chart polishing to note here:</p>
<ul>
<li>Colour filling in the wedges follows a natural sequence over time</li>
<li>Colour of text is on a scale in the reverse direction to the colour of the fill, to improve readability</li>
<li>Wedges advance in time clockwise rather than anti-clockwise, more intuitive for most readers</li>
<li>Order of legend chosen to follow the natural sequence of the eye following the wedges around</li>
<li>Pale polar co-ordinates gridlines left in place, but other unnecessary aixs numbers, ticks and titles removed</li>
<li>Interpretive title, and caption showing the source</li>
</ul>
<p>Here’s the code using R and <code>ggplot2</code>. Of course, a pie chart is just a bar chart with polar co-ordinates:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span># http://www.stuff.co.nz/national/86458731/Cheviot-earthquake-Tracing-the-source-of-the-7-5-magnitude-quake-and-its-aftermath?utm_source=dlvr.it&amp;utm_medium=twitter
</span><span>
</span><span>eqs</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>energy</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>48.99</span><span>,</span><span> </span><span>13.34</span><span>,</span><span> </span><span>2.67</span><span>,</span><span> </span><span>6.25</span><span>,</span><span> </span><span>4.93</span><span>,</span><span> </span><span>2.71</span><span>,</span><span> </span><span>4.73</span><span>,</span><span> </span><span>16.38</span><span>),</span><span>
</span><span>period</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Nov 14 2016&quot;</span><span>,</span><span> </span><span>&quot;Earlier 2016&quot;</span><span>,</span><span> </span><span>paste</span><span>(</span><span>&quot;All&quot;</span><span>,</span><span> </span><span>2015</span><span>:</span><span>2010</span><span>)),</span><span>
</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>eqs</span><span> </span><span>&lt;-</span><span> </span><span>eqs</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>period</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>period</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>period</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>period</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>cumenergy</span><span> </span><span>=</span><span> </span><span>cumsum</span><span>(</span><span>energy</span><span>),</span><span>
</span><span>centres</span><span> </span><span>=</span><span> </span><span>cumenergy</span><span> </span><span>-</span><span> </span><span>energy</span><span> </span><span>/</span><span> </span><span>2</span><span>)</span><span>
</span><span>leg</span><span> </span><span>&lt;-</span><span> </span><span>guide_legend</span><span>(</span><span>reverse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>eqs</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>energy</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>period</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>1.3</span><span>,</span><span> </span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>centres</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>energy</span><span>,</span><span> </span><span>&quot;%&quot;</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>period</span><span>))</span><span> </span><span>+</span><span>
</span><span>coord_polar</span><span>(</span><span>theta</span><span> </span><span>=</span><span> </span><span>&quot;y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Oranges&quot;</span><span>,</span><span> </span><span>direction</span><span> </span><span>=</span><span> </span><span>-1</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>leg</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_color_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Blues&quot;</span><span>,</span><span> </span><span>direction</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>leg</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.ticks</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>plot.caption</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: http://www.stuff.co.nz/national/86458731/\nCheviot-earthquake-Tracing-the-source-of-the-7-5-magnitude-quake-and-its-aftermath\nSupplied by John Holdaway&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Half the earthquake energy released since 2010 came in a single day&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Energy released in all New Zealand earthquakes, 2010-2016&quot;</span><span>)</span></code></pre>
</figure>
<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/extreme-pie-chart-polishing/"></div></div><div><a data-url="https://www.r-bloggers.com/extreme-pie-chart-polishing/" data-counturl="https://www.r-bloggers.com/extreme-pie-chart-polishing/" data-count="vertical" data-text="Extreme pie chart polishing" 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://ellisp.github.io/blog/2016/11/15/piecharts"> Peter's stats stuff - R</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/ZpWXUVXv9HU"></a></div>