<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/scatterpie-for-plotting-pies-on-ggplot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2016/12/scatterpie-for-plotting-pies-on-ggplot"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="" src="https://i2.wp.com/guangchuangyu.github.io/blog_images/R/scatterpie_files/so.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Plotting pies on ggplot/ggmap is not an easy task, as ggplot2 doesn’t provide native pie geom. The pie we produced in ggplot2 is actually a barplot transform to polar coordination. This make it difficult if we want to produce a map like the above screenshot, which was posted by Tyler Rinker, the author of R package pacman.</p>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/R/scatterpie_files/twitter.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The question remained unsolved until he discover that ggtree can do it. The ggtree solution is to use the <code>subview</code> function, which is good for embed subplots and can embed different types of plots and even user’s own image files.</p>
<p>But it has its own drawback for plotting pies on map. First, it render plots as raster image make it slow to render when we plotting a lot of pies. Second we need some hack to add legend.</p>
<p>Thanks to the <code>ggforce</code> package, which provide a native implementation of the pie geom, we can plot pies on cartesian coordination.</p>
<p>I created a wrapper function to make it more easy to plot a set of pies.</p>
<p>For example, suppose we have the following data:</p>
<pre><code>set.seed(123)
long &lt;- rnorm(50, sd=100)
lat &lt;- rnorm(50, sd=50)
d &lt;- data.frame(long=long, lat=lat)
d &lt;- with(d, d[abs(long) &lt; 150 &amp; abs(lat) &lt; 70,])
n &lt;- nrow(d)
d$region &lt;- factor(1:n)
d$A &lt;- abs(rnorm(n, sd=1))
d$B &lt;- abs(rnorm(n, sd=2))
d$C &lt;- abs(rnorm(n, sd=3))
d$D &lt;- abs(rnorm(n, sd=4))
d$radius &lt;- 6 * abs(rnorm(n))
head(d)

##          long        lat region          A        B        C        D
## 1  -56.047565  12.665926      1 0.71040656 2.887786 1.309570 2.892264
## 2  -23.017749  -1.427338      2 0.25688371 1.403569 1.375096 4.945092
## 4    7.050839  68.430114      3 0.24669188 0.524395 3.189978 5.138863
## 5   12.928774 -11.288549      4 0.34754260 3.144288 3.789556 2.295894
## 8 -126.506123  29.230687      5 0.95161857 3.029335 1.048951 2.471943
## 9  -68.685285   6.192712      6 0.04502772 3.203072 2.596539 4.439393
##      radius
## 1 6.4847970
## 2 3.7845247
## 4 0.6818394
## 5 9.1974120
## 8 3.1267039
## 9 2.9392227
</code></pre>
<p>It is very easy to draw the pies on the map by the <code>geom_scatterpie</code> layer.</p>
<pre><code>library(ggplot2)
library(scatterpie)

world &lt;- map_data('world')
p &lt;- ggplot(world, aes(long, lat)) +
    geom_map(map=world, aes(map_id=region), fill=NA, color=&quot;black&quot;) +
    coord_quickmap()
p + geom_scatterpie(aes(x=long, y=lat, group=region, r=radius),
                    data=d, cols=LETTERS[1:4], color=NA, alpha=.8) +
    geom_scatterpie_legend(d$radius, x=-160, y=-55)
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/R/scatterpie_files/scatterpie.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This is just a simple application of the <code>ggforce</code>, and I find many people like it. They even asked me to implement the pie size legend. I do implemented a <code>geom_scatterpie_legend</code> layer and as the name indicated, it add a legend of the pie sizes as demonstrated in the above figure.</p>
<p>The source code is quite simple, and it is impossible without <code>ggforce</code>. Now this package is availabel on <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=scatterpie">CRAN</a>, you can use <code>install.packages('scatterpie')</code> to install it and visit the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/scatterpie/vignettes/scatterpie.html">online vignette</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://guangchuangyu.github.io/2016/12/scatterpie-for-plotting-pies-on-ggplot"> R on Guangchuang YU</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/reEEwKB6o88"></a></div>