<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/plotting-scottish-census-data-with-some-tidyverse-magic/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2018/11/28/plotting-scottish-census-data-with-some-tidyverse-magic/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’ve been working with the Scottish census recently, to investigate employment in land-based (agriculture, forestry and fishing) industry. A friend of mine has recently moved to Dumfries and Galloway – a rural, farming area of Scotland. He’s commented on the ageing population in the area, so I pulled out the age profile from the census for his <a rel="nofollow" href="https://www.nrscotland.gov.uk/statistics-and-data/geography/our-products/other-national-records-of-scotland-nrs-geographies-datasets" target="_blank">civil parish</a>. This post shows how to plot up an age profile from the Scottish census table KS102SC, which is <a rel="nofollow" href="https://www.scotlandscensus.gov.uk/ods-web/data-warehouse.html" target="_blank">available online</a>.</p>
<p>First up, let’s load our packages and read in the table. Note I’ve skipped the first few header lines and have coded – to NA. In reality – are actually 0s, so I’ve used `mutate_all` to fix them.</p>
<pre>
library(tidyverse)

df = read_csv(&quot;~/Downloads/temp/KS102SC.csv&quot;, skip=4, na=&quot;-&quot;) %>%
   mutate_all(funs(replace(., is.na(.), 0)))
</pre>
<p>Next we can select the parish of interest, select the columns we’re interested in, convert these to long format, and force the ordering of the ages (e.g. 8-10 should come before 10-14). I’ve piped the output of this munging into ggplot and added some styling and an all important licence statement.</p>
<p><a rel="nofollow" href="https://scottishsnow.files.wordpress.com/2018/11/dalton.png" target="_blank"><img data-orig-file="https://scottishsnow.files.wordpress.com/2018/11/dalton.png?w=456" data-orig-size="1078,600" data-medium-file="https://scottishsnow.files.wordpress.com/2018/11/dalton.png?w=456?w=300" data-attachment-id="1596" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2018/11/dalton.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2018/11/28/plotting-scottish-census-data-with-some-tidyverse-magic/dalton/" data-large-file="https://scottishsnow.files.wordpress.com/2018/11/dalton.png?w=456?w=620" alt="Dalton" data-image-title="Dalton" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1596" data-recalc-dims="1"></a></p>
<pre>
df %>%
   filter(X1==&quot;Dalton&quot;) %>%
   select(-X1, -`All people`, -`Mean age`, -`Median age`, -X21) %>%
   gather() %>%
   mutate(key = reorder(key, seq_along(key))) %>%
   ggplot(aes(key, value)) +
   geom_col() +
   labs(title=&quot;Dalton parish population distribution&quot;,
        subtitle=&quot;Contains: Scotland's Census data and Scottish Government data\nlicensed under the Open Government Licence v3.0&quot;,
        x=&quot;&quot;,
        y=&quot;People&quot;) +
   coord_flip() +
   theme_bw() +
   theme(text=element_text(size=20),
         plot.subtitle=element_text(size=10))
</pre>
<p>It’s also of interest to compare one parish against another, so I compared Dalton against Edinburgh. Basically as before but adding an extra point layer for the visualisation. The data have now been changed to proportions of each parish so they are comparable.</p>
<p><a rel="nofollow" href="https://scottishsnow.files.wordpress.com/2018/11/dalton_edinburgh.png" target="_blank"><img data-orig-file="https://scottishsnow.files.wordpress.com/2018/11/dalton_edinburgh.png?w=456" data-orig-size="1078,600" data-medium-file="https://scottishsnow.files.wordpress.com/2018/11/dalton_edinburgh.png?w=456?w=300" data-attachment-id="1597" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2018/11/dalton_edinburgh.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2018/11/28/plotting-scottish-census-data-with-some-tidyverse-magic/dalton_edinburgh/" data-large-file="https://scottishsnow.files.wordpress.com/2018/11/dalton_edinburgh.png?w=456?w=620" alt="Dalton_Edinburgh" data-image-title="Dalton_Edinburgh" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1597" data-recalc-dims="1"></a></p>
<pre>
x = df %>%
   filter(X1==&quot;Dalton&quot; | X1==&quot;Edinburgh&quot;) %>%
   select(-`Mean age`, -`Median age`, -X21) %>%
   mutate_at(vars(-X1), funs(prop = . / `All people`)) %>%
   select(-`All people_prop`) %>%
   select(X1, ends_with(&quot;prop&quot;)) %>%
   gather(key, value, -X1) %>%
   separate(key, c(&quot;key&quot;, &quot;drop&quot;), &quot;_&quot;) %>%
   mutate(key = reorder(key, seq_along(key)))

x %>%
   filter(X1==&quot;Dalton&quot;) %>%
   ggplot(aes(key, value)) +
   geom_col() +
   geom_point(data=filter(x, X1==&quot;Edinburgh&quot;), aes(key, value)) +
   scale_y_continuous(labels=scales::percent) +
   labs(title=&quot;Dalton parish (bars) and Edinburgh (dots) population distribution&quot;,
        subtitle=&quot;Contains: Scotland's Census data and Scottish Government data\nlicensed under the Open Government Licence v3.0&quot;,
        x=&quot;&quot;,
        y=&quot;People&quot;) +
   coord_flip() +
   theme_bw() +
   theme(text=element_text(size=20),
         plot.subtitle=element_text(size=10))
</pre>
<p>Finally, we can compare distributions for the whole of Scotland against Edinburgh and Dalton using boxplots. I can imagine a beautiful plot with density polygons showing the national data, but I don’t have time to figure it out now!</p>
<p><a rel="nofollow" href="https://scottishsnow.files.wordpress.com/2018/11/boxplots.png" target="_blank"><img data-orig-file="https://scottishsnow.files.wordpress.com/2018/11/boxplots.png?w=456" data-orig-size="1078,600" data-medium-file="https://scottishsnow.files.wordpress.com/2018/11/boxplots.png?w=456?w=300" data-attachment-id="1595" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2018/11/boxplots.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2018/11/28/plotting-scottish-census-data-with-some-tidyverse-magic/boxplots/" data-large-file="https://scottishsnow.files.wordpress.com/2018/11/boxplots.png?w=456?w=620" alt="boxplots" data-image-title="boxplots" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1595" data-recalc-dims="1"></a></p>
<pre>
x = df %>%
   select(-`Mean age`, -`Median age`, -X21) %>%
   mutate_at(vars(-X1), funs(prop = . / `All people`)) %>%
   select(-`All people_prop`) %>%
   select(X1, ends_with(&quot;prop&quot;)) %>%
   gather(key, value, -X1) %>%
   separate(key, c(&quot;key&quot;, &quot;drop&quot;), &quot;_&quot;) %>%
   mutate(key = reorder(key, seq_along(key)))

x %>%
   filter(X1!=&quot;Scotland&quot;) %>%
   ggplot(aes(key, value)) +
   geom_boxplot(colour=&quot;grey50&quot;) +
   geom_point(data=filter(x, X1==&quot;Dalton&quot;), aes(key, value), colour=&quot;purple4&quot;, shape=4, stroke=2, show.legend=T) +
   geom_point(data=filter(x, X1==&quot;Edinburgh&quot;), aes(key, value), colour=&quot;darkorange2&quot;, shape=2, stroke=1.5, show.legend=T) +
   scale_y_continuous(labels=scales::percent) +
   labs(title=&quot;Dalton parish (purple crosses) and Edinburgh (orange triangles)\nover Scotland's population distribution&quot;,
        subtitle=&quot;Contains: Scotland's Census data and Scottish Government data\nlicensed under the Open Government Licence v3.0&quot;,
        x=&quot;&quot;,
        y=&quot;People&quot;) +
   coord_flip() +
   theme_bw() +
   theme(text=element_text(size=20),
         plot.subtitle=element_text(size=10))
</pre>
	<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://scottishsnow.wordpress.com/2018/11/28/plotting-scottish-census-data-with-some-tidyverse-magic/"> R – scottishsnow</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/MsBIULkV5a0" height="1"></a></div>