<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/spring-budget-2017-circle-visualisation/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/spring-budget-2017-circle-visualisation/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It’s time to branch out into a new area of data visualisation: <a rel="nofollow" href="http://datavizproject.com/data-type/proportional-area-chart-circle/" target="_blank">proportion area plots</a>. These plots use area to show proportion between different related values. A common type of proportional area plots are <a rel="nofollow" href="https://en.wikipedia.org/wiki/Treemapping" target="_blank">tree maps</a>. We are going to be using the same principle but with circles.</p>
<p>A common <em>subject</em> for area visualisation is budgets and money, which will be the subject of this post.</p>
<h2>We are going to take a look at the Spring Budget 2017 in the UK.</h2>
<p>Page <a rel="nofollow" href="https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/597467/spring_budget_2017_web.pdf" target="_blank">four of the document</a> has two pie charts detailing how public money will be spent this year and where it will come from.</p>
<p><a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/10/spring-budget-2017.png?ssl=1" target="_blank"><img sizes="(max-width: 336px) 100vw, 336px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/10/spring-budget-2017-336x490.png?resize=336%2C490&ssl=1" alt="" class="wp-image-9177" data-recalc-dims="1"></a></p>
<p>I couldn’t find this data in any tables, so we will have to create our data frames manually:</p>
<pre> spending &lt;- data.frame(c(&quot;Social protection&quot;,&quot;Health&quot;,&quot;Education&quot;,&quot;Other&quot;,&quot;Defence&quot;,&quot;Debt interest&quot;,&quot;Transport&quot;,&quot;Housing\n &amp; environment&quot;,&quot;Public order&quot;,&quot;Social\n services&quot;,&quot;Industry&quot;),
 c(245,149,102,50,48,46,37,36,34,32,23),
 c(802,802,802,802,802,802,802,802,802,802,802))
 names(spending) &lt;- c(&quot;Expenditure&quot;,&quot;Spending&quot;,&quot;Total&quot;)
income &lt;- data.frame(c(&quot;Income tax&quot;,&quot;VAT&quot;,&quot;National Insurance&quot;, &quot;Other taxes&quot;,&quot;Borrowing&quot;,&quot;Other (non taxes)&quot;,&quot;Corporation Tax&quot;,&quot;Excise duties&quot;,&quot;Council tax&quot;,&quot;Business\n rates&quot;),
 c(175,143,130,80,58,54,52,48,32,30),
 c(802,802,802,802,802,802,802,802,802,802))
 names(income) &lt;- c(&quot;Source&quot;,&quot;Income&quot;,&quot;Total&quot;)</pre>
<p>I am including ‘borrowing’ in the Government’s income section, although it will be the only circle that won’t be a tax.</p>
<p>To draw our circles we will be using the <a rel="nofollow" href="https://github.com/mbedward/packcircles" target="_blank">packcircles</a> package.</p>
<pre>library(&quot;packcircles&quot;)
library(&quot;ggplot2&quot;)
options(scipen = 999)
library(extrafont)
library(extrafontdb)
loadfonts()
fonts()

t &lt;- theme_bw() + 
 theme(panel.grid = element_blank(), 
 axis.text=element_blank(),
 axis.ticks=element_blank(), 
 axis.title=element_blank())</pre>
<p>The first thing to do is to use the functions within the package to generate the radii and coordinates, as per <a rel="nofollow" href="https://rdrr.io/cran/packcircles/man/circleProgressiveLayout.html" target="_blank">this code here</a>.</p>
<pre># circle areas
areas &lt;- income$Income 
areas2 &lt;- spending$Spending
# income
packing1 &lt;- circleProgressiveLayout(areas)
dat1 &lt;- circleLayoutVertices(packing1)
# spending
packing2 &lt;- circleProgressiveLayout(areas2) 
dat2 &lt;- circleLayoutVertices(packing2)</pre>
<p>Next up we are going to move to the visualisation. It would be more informative if we could display income and expenditure side-by-side.</p>
<p>Like last time when we compared <a rel="nofollow" href="http://rforjournalists.com/2017/09/10/comparing-donald-trump-and-hillary-clintons-facebook-pages-during-the-us-presidential-election-2016/" target="_blank">Donald Trump and Hillary Clinton on Facebook</a>, we can use facetting for this purpose in ggplot2.</p>
<p>I adapted <a rel="nofollow" href="https://lastresortsoftware.blogspot.com/2017/04/packcircles-version-020-released.html" target="_blank">this code</a> for this purpose.</p>
<pre>dat &lt;- rbind(
cbind(dat1, set = 1),
cbind(dat2, set = 2) )
p &lt;- ggplot(data = dat, aes(x, y)) + 
geom_polygon(aes(group = id, fill = -set), 
colour = &quot;black&quot;, show.legend = FALSE) + theme(text = element_text(family=&quot;Browallia New&quot;, size = 50)) +
coord_equal() + ggtitle(&quot;Income and expenditure for the UK Government&quot;) +
scale_colour_manual(values = c(&quot;white&quot;,&quot;White&quot;)) +
facet_grid(~set, 
labeller = as_labeller(
c('1' = &quot;Income&quot;, '2' = &quot;Expenditure&quot;)))</pre>
<p>With no labels:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/circles.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/10/circles-490x261.png?w=450&ssl=1" alt="" class="wp-image-9187" data-recalc-dims="1"></a></p>
<p>This is looking good, but obviously we have no way of knowing which circle corresponds to which area of income or expenditure. In ggplot2 you can use <code>geom_text()</code> to add annotations. In order for them to be positioned correctly, we need to build a relationship between the labels and the circles. We can do this by creating centroids from the coordinates we already have for the circles.</p>
<p>If we have accurate centroid coordinates then our labels will always appear in the centre of our circles.</p>
<p>Going back to our <code>dat</code> data frame:</p>
<pre>str(dat)
<em>> str(dat)'data.frame': 546 obs. of  4 variables:
$ x  : num  0 -0.277 -1.092 -2.393 -4.099 ...
$ y  : num  0 2.2 4.25 6.05 7.46 ...
$ id : int  1 1 1 1 1 1 1 1 1 1 ...
$ set: num  1 1 1 1 1 1 1 1 1 1 ...</em></pre>
<p>Our <code>ID</code> column shows which circle it belongs to, and our set column shows whether it belongs in income or expenditure. From this we can deduce that each circle is plotted using 26 pairs of x/y coordinates (at row 27 the ID changes from one to two).</p>
<p>The mean of each 26 pairs is the centre of the corresponding circle.</p>
<p>Our <code>dat</code> data frame is 546 rows long. We need a sequence of numbers, 26 apart, going up to 546:</p>
<pre>sequence &lt;- data.frame(c(x = (seq(from = 1, to = 546, by = 26))), y = seq(from = 26, to = 547, by = 26))
names(sequence)  head(sequence)
<em> x y
x1 1 26
x2 27 52
x3 53 78
x4 79 104
x5 105 130
x6 131 156</em></pre>
<p>Next up we will create a data frame that will contain our centroids:</p>
<pre>centroid_df &lt;- data.frame(seq(from = 1, to = 521, by = 1))
names(centroid_df) &lt;- c(&quot;number&quot;)</pre>
<p>There are probably a few different ways to run the next section, probably a bit more elegantly than what we’re about to do as well. </p>
<p>This for loop will calculate the mean of every single combination of 26 coordinates and store the results in the new <code>centroid_df</code>.</p>
<pre>for (i in 1:521) {
      j = i + 25
      centroid_df$x[i] &lt;- mean(dat$x[i:j])
      centroid_df$y[i] &lt;- mean(dat$y[i:j])
 }</pre>
<p>Now we bring in our <code>sequence</code> data frame to filter out the ones we don’t need in a new coords data frame, leaving just the ones that correspond correctly to the circles:</p>
<pre>coords &lt;- merge(sequence,centroid_df, by.x = &quot;x&quot;, by.y = &quot;number&quot;)
names(coords) &lt;- c(&quot;number1&quot;,&quot;number2&quot;,&quot;x&quot;,&quot;y&quot;)</pre>
<p>Next up for labelling purposes is to clarify which set each pair of coordinates belongs to:</p>
<pre>coords$set[1:21] &lt;- 1 
coords$set[11:21] &lt;- 2 #this overrides some of the values set in the line above</pre>
<p>Finally we will paste in the names and amounts from our original income and spending data frames into a new label column:</p>
<pre>coords$label &lt;- NA
coords$label[1:21] &lt;- paste(income$Source,&quot;\n£&quot;,income$Income,&quot;bn&quot;)
coords$label[11:21] &lt;- paste(spending$Expenditure,&quot;\n£&quot;,spending$Spending,&quot;bn&quot;)
coords$label &lt;- gsub(&quot;£ &quot;,&quot;£&quot;,coords$label)
coords$label &lt;- gsub(&quot; bn&quot;,&quot;bn&quot;,coords$label)</pre>
<p>Now let’s add in the labels:</p>
<pre>p &lt;- p+ geom_text(data = coords, aes(x = x, y = y, label = label, colour = factor(set)),show.legend = FALSE)
p</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/circles_labelled.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/10/circles_labelled-490x261.png?w=450&ssl=1" alt="" class="wp-image-9190" data-recalc-dims="1"></a></p>
<p>There we have it, some nicely formatted labels showing what each circle refers to, and how much money it represents.</p>
<h2>Brief analysis</h2>
<p>On the left hand side we see that income tax is the biggest cash cow for the Government. Incidentally, about 28 per cent of that is paid for by the top 1% of earners:</p>
<blockquote>
<p lang="en" dir="ltr">The top 5% are liable for almost half the UK's total income tax. </p>
<p>Not enough for Jeremy Corbyn, so what would he like it to be? <a rel="nofollow" href="https://t.co/poeC2N6Sn9" target="_blank">pic.twitter.com/poeC2N6Sn9</a></p>
<p>— Rob Grant (@robgrantuk) <a rel="nofollow" href="https://twitter.com/robgrantuk/status/877540060832387072?ref_src=twsrc%5Etfw" target="_blank">June 21, 2017</a></p>
</blockquote>
<p>Social protection means benefits, essentially. A huge chunk of that is the State Pension, followed by various working-age benefits and tax credits.</p>
<p>Notice that as a country we spend almost as much paying off the interest on our debts as we do on our own defence.</p>
<p>Health and education – those two ‘classic’ public services – cost a combined £250bn.</p>
<h2>Conclusion</h2>
<p>I am a fan of using these circles to visualise this kind of data, especially money.</p>
<p>If you have lots of networked data (for instance, if you had breakdowns of how exactly the social protection budget will be spent) then ggraph would be a good option for you to produce plots like this:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/10/2000px-Relative_neighborhood_graph.svg_-300x300.png?ssl=1" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/10/2000px-Relative_neighborhood_graph.svg_-300x300.png?resize=300%2C300&ssl=1" alt="" class="wp-image-9191" data-recalc-dims="1"></a></p>
<p>Comment below if you have questions!</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/qualitative-research-in-r/" title="Permanent Link toQualitative Research in R" target="_blank">Qualitative Research in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/multi-dimensional-reduction-and-visualisation-with-t-sne/" title="Permanent Link toMulti-Dimensional Reduction and Visualisation with t-SNE" target="_blank">Multi-Dimensional Reduction and Visualisation with t-SNE</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/comparing-trump-and-clintons-facebook-pages-during-the-us-presidential-election-2016/" title="Permanent Link toComparing Trump and Clinton’s Facebook pages during the US presidential election, 2016" target="_blank">Comparing Trump and Clinton’s Facebook pages during the US presidential election, 2016</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/analyzing-obesity-across-usa/" title="Permanent Link toAnalyzing Obesity across USA" target="_blank">Analyzing Obesity across USA</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/can-we-predict-flu-deaths-with-machine-learning-and-r/" title="Permanent Link toCan we predict flu deaths with Machine Learning and R?" target="_blank">Can we predict flu deaths with Machine Learning and R?</a></li>
</ol>
	<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://datascienceplus.com/spring-budget-2017-circle-visualisation/"> R Programming – DataScience+</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/il-BF3mcl9g" height="1"></a></div>