<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/luke-warm-about-micromaps/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/04/30/micromaps"> 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>Continuing my exploring methods for spatial visualisation of data in R, today I’m looking at <a target="_blank" rel="nofollow" href="https://gis.cancer.gov/tools/micromaps/">linked micromaps</a>. Micromaps are a way of showing statistical graphics for a small subset of regions at a time, with a small map indicating which regions are being looked at in each of the small multiples. Alberto Cairo has some nice discussion in <a target="_blank" rel="nofollow" href="http://www.thefunctionalart.com/2013/07/falling-in-love-with-micromaps.html">this blog post</a>.</p>
<h2>Poverty and education in the USA</h2>
<p>It’s easier to show than explain. Here’s one I’ve adapted from the helpfiles of the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=micromap">R micromaps package</a> by Quinn Payton, Tony Olsen, Marc Weber, Michael McManus and Tom Kincaid of the US Environmental Protection Agency and generously open-sourced by that agency. Under the hood, <code>micromaps</code> uses Hadley Wickham’s <code>ggplot2</code> to create the graphic assets and (I think) Paul Murrell’s <code>grid</code> to lay them out in aligned fashion.</p>
<p><img src="https://i2.wp.com/ellisp.github.io/img/0095-eg1.png?w=100%25" data-recalc-dims="1"></p>
<p>Some links for the <code>micromaps</code> package:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=micromap">Production version</a> on CRAN</li>
<li><a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v063i02">Introductory article</a> in the Journal of Statistical Software</li>
<li><a target="_blank" rel="nofollow" href="https://github.com/USEPA/R-micromap-package-development">Source code</a> on the USEPA GitHub page</li>
</ul>
<p>I used this USA example as my test case for understanding the <code>micromaps</code> API. Some of the changes I’ve introduced here include:</p>
<ul>
<li>Set my own font</li>
<li>Pale blue background with white gridlines to make them go into the background, ggplot2-style.</li>
<li>White borders for the grey-fill states that have been previously drawn, so the border lines don’t distract the eye from the five states being referred to in each cluster.</li>
<li>Tidied up the code for readability</li>
</ul>
<p>Here’s the code that draws this, and sets up the overall session. Note that the <code>nzcensus</code> (by me) and <code>mbiemaps</code> (by New Zealand’s Ministry of Business, Innovation and Employment, my previous employer) are only available on GitHub, not CRAN. Code to install them is available in a <a target="_blank" rel="nofollow" href="http://127.0.0.1:4000/blog/2017/04/23/cartograms">previous post</a>. They’re needed for New Zealand data and maps later on.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>micromap</span><span>)</span><span>
</span><span>library</span><span>(</span><span>nzcensus</span><span>)</span><span>
</span><span>library</span><span>(</span><span>extrafont</span><span>)</span><span>
</span><span>library</span><span>(</span><span>mbiemaps</span><span>)</span><span> </span><span># for the territorial authority map
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>testthat</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggrepel</span><span>)</span><span>
</span><span>the_font</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Calibri&quot;</span><span>
</span><span>theme_set</span><span>(</span><span>theme_minimal</span><span>(</span><span>base_family</span><span> </span><span>=</span><span> </span><span>the_font</span><span>))</span><span>
</span><span>#============polish up the example from the ?lmplot helpfile===============
</span><span>data</span><span>(</span><span>&quot;USstates&quot;</span><span>)</span><span>
</span><span>data</span><span>(</span><span>&quot;edPov&quot;</span><span>)</span><span>
</span><span># convert from SpatialPolygonsDataFrame into a data frame (similar to fortify)
</span><span>statePolys</span><span> </span><span>&lt;-</span><span> </span><span>create_map_table</span><span>(</span><span>USstates</span><span>,</span><span> </span><span>'ST'</span><span>)</span><span>
</span><span># draw graphic
</span><span>lmplot</span><span>(</span><span>stat.data</span><span> </span><span>=</span><span> </span><span>edPov</span><span>,</span><span>
</span><span>map.data</span><span> </span><span>=</span><span> </span><span>statePolys</span><span>,</span><span>
</span><span>panel.types</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'labels'</span><span>,</span><span> </span><span>'dot'</span><span>,</span><span> </span><span>'dot'</span><span>,</span><span>'map'</span><span>),</span><span>
</span><span>panel.data</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>'state'</span><span>,</span><span>'pov'</span><span>,</span><span>'ed'</span><span>,</span><span> </span><span>NA</span><span>),</span><span>
</span><span>ord.by</span><span> </span><span>=</span><span> </span><span>'pov'</span><span>,</span><span> </span><span>grouping</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span>
</span><span>colors</span><span> </span><span>=</span><span> </span><span>brewer.pal</span><span>(</span><span>5</span><span>,</span><span> </span><span>&quot;Spectral&quot;</span><span>),</span><span>
</span><span>median.row</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span># how to merge the two data frames:
</span><span> </span><span>map.link</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'StateAb'</span><span>,</span><span> </span><span>'ID'</span><span>),</span><span>
</span><span># stroke colour of the borders of previously-drawn areas:
</span><span> </span><span>map.color2</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span>
</span><span># how to save the result:
</span><span> </span><span>print.file</span><span> </span><span>=</span><span> </span><span>&quot;0095-eg1.png&quot;</span><span>,</span><span>
</span><span>print.res</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span>
</span><span>plot.width</span><span> </span><span>=</span><span> </span><span>7</span><span>,</span><span>
</span><span>plot.height</span><span> </span><span>=</span><span> </span><span>9</span><span>,</span><span>
</span><span># attributes of the panels:
</span><span> </span><span>panel.att</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>list</span><span>(</span><span>1</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;States&quot;</span><span>,</span><span> </span><span>panel.width</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>text.size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>align</span><span> </span><span>=</span><span> </span><span>'right'</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>text.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>left.margin</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>right.margin</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>list</span><span>(</span><span>2</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Percent living below\npoverty level&quot;</span><span>,</span><span> </span><span>xaxis.title</span><span> </span><span>=</span><span> </span><span>'Percent'</span><span>,</span><span>
</span><span>xaxis.ticks</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>10</span><span>,</span><span> </span><span>15</span><span>,</span><span> </span><span>20</span><span>),</span><span>
</span><span>xaxis.labels</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>10</span><span>,</span><span> </span><span>15</span><span>,</span><span> </span><span>20</span><span>),</span><span>
</span><span>graph.bgcolor</span><span> </span><span>=</span><span> </span><span>'lightblue'</span><span>,</span><span>
</span><span>graph.grid.color</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span>
</span><span>graph.border.color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>),</span><span>
</span><span>list</span><span>(</span><span>3</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Percent adults with\n4+ years of college&quot;</span><span>,</span><span> </span><span>xaxis.title</span><span> </span><span>=</span><span> </span><span>'Percent'</span><span>,</span><span>
</span><span>xaxis.ticks</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>0</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>30</span><span>,</span><span> </span><span>40</span><span>),</span><span>
</span><span>xaxis.labels</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>0</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>30</span><span>,</span><span> </span><span>40</span><span>),</span><span>
</span><span>graph.bgcolor</span><span> </span><span>=</span><span> </span><span>'lightblue'</span><span>,</span><span>
</span><span>graph.grid.color</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span>
</span><span>graph.border.color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>),</span><span>
</span><span>list</span><span>(</span><span>4</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>'Light gray means\nhighlighted above'</span><span>,</span><span> </span><span>panel.width</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span>
</span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;italic&quot;</span><span>)</span><span>
</span><span>))</span></code></pre>
</figure>
<p>Most of the polishing code is in the list of lists passed to the <code>panel.att</code> argument. Each list refers to the attributes of one of the four panels (state names, poverty dot charts, education dot charts, maps). I had to do a bit of digging to find out how to control things like grid colour; while doing this it was useful to run one of the three lines of code below to see what attributes are within your control for the different panel types:</p>
<figure>
<pre><code data-lang="r"><span>unlist</span><span>(</span><span>labels_att</span><span>(</span><span>TRUE</span><span>)</span><span> </span><span>)</span><span>
</span><span>unlist</span><span>(</span><span>dot_att</span><span>(</span><span>TRUE</span><span>)</span><span> </span><span>)</span><span>
</span><span>unlist</span><span>(</span><span>map_att</span><span>(</span><span>TRUE</span><span>)</span><span> </span><span>)</span></code></pre>
</figure>
<p>Note that the <code>lmplot</code> function specifies a file to print the graphic to. I don’t like this, as it’s breaks some commonly accepted R workflows. For example, for this blog I usually create in advance all the graphics for each post in SVG format, which scales up nicely if people zoom in on it and is generally the best format (my view) for web graphics. That can’t be done when <code>lmplot</code> restricts you to particular device types. The pattern also doesn’t work well with Yixuan Qiu’s <a target="_blank" rel="nofollow" href="https://github.com/yixuan/showtext"><code>showtext</code> R package</a> that I normally use for fonts (it lets me access Google fonts, including the Poppins font I use for most graphics).</p>
<h2>New Zealand census example</h2>
<p>To be sure I understood how to use the technique, I wanted to apply it to some New Zealand maps and data. I’m used to presenting data at the Territorial Authority level in New Zealand by means of a choropleth map like this one:</p>
<p><img src="https://i1.wp.com/ellisp.github.io/img/0095-choropleth.png?w=100%25" data-recalc-dims="1"></p>
<p>… which was drawn with this code, using the <code>TA2013</code> data frame of 2013 census data from my <code>nzcensus</code> package:</p>
<figure>
<pre><code data-lang="r"><span># pre-fortified data frame version of TA level map, from mbiemaps package:
</span><span>data</span><span>(</span><span>ta_simpl_gg</span><span>)</span><span>
</span><span># change name in census data to match the name used in ta_simpl_gg
</span><span>TA2013</span><span>$</span><span>short_name</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot; District&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>TA2013</span><span>$</span><span>TA2013_NAM</span><span>)</span><span>
</span><span># filter out some visually inconvenient data:
</span><span>ta_data</span><span> </span><span>&lt;-</span><span> </span><span>TA2013</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>short_name</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Chatham Islands Territory&quot;</span><span>,</span><span> </span><span>&quot;Area Outside Territorial Authority&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>PercNoReligion</span><span> </span><span>=</span><span> </span><span>PropNoReligion2013</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span>
</span><span># draw map: </span><span>ta_simpl_gg</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>ta_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;NAME&quot;</span><span> </span><span>=</span><span> </span><span>&quot;short_name&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>order</span><span>)</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>MedianIncome2013</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_polygon</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>ggmap</span><span>::</span><span>theme_nothing</span><span>(</span><span>legend</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.2</span><span>,</span><span> </span><span>0.7</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_fill_gradientn</span><span>(</span><span>&quot;Median individual\nincome&quot;</span><span>,</span><span> </span><span>colours</span><span> </span><span>=</span><span> </span><span>brewer.pal</span><span>(</span><span>11</span><span>,</span><span> </span><span>&quot;Spectral&quot;</span><span>),</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>dollar</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_map</span><span>(</span><span>projection</span><span> </span><span>=</span><span> </span><span>&quot;sinusoidal&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Example choropleth map&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Statistics New Zealand, 2013 Census&quot;</span><span>)</span></code></pre>
</figure>
<p>Doing this with a linked micromap instead of a choropleth map lets us look at more variables at once, but I can’t say I’m happy with the result:</p>
<p><img src="https://i2.wp.com/ellisp.github.io/img/0095-eg2.png?w=100%25" data-recalc-dims="1"></p>
<p>My reservations about this graphic:</p>
<ul>
<li>It feels like there are just too many territorial authorities for this to be a really friendly graphic.</li>
<li>Also, my map of New Zealand is probably too crinkly and individual districts and cities too small to show up well.</li>
<li>There’s an awkwardness of New Zealand being tall rather than wide – a smaller aspect ratio than USA. This seems to make the New Zealand map less well suited to the technique than the USA map.</li>
<li>It’s hard for the reader to move their eyes back and forth from the district or city name to the dots and to the map.</li>
<li>I couldn’t work out how (if it is possible) to control the projection of the map, and hence New Zealand looks a little bit stretched and rotated.</li>
</ul>
<p>Here’s the code that drew this, anyway:</p>
<figure>
<pre><code data-lang="r"><span># change names of the map data frame to meet lmplot's expectations
</span><span>ta_polys2</span><span> </span><span>&lt;-</span><span> </span><span>ta_simpl_gg</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>coordsx</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>coordsy</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>ID</span><span> </span><span>=</span><span> </span><span>NAME</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>hole</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>hole</span><span>),</span><span>
</span><span>plug</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span>
</span><span>region</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>as.factor</span><span>(</span><span>ID</span><span>)))</span><span>
</span><span># check merge will be ok
</span><span>a</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>ta_polys2</span><span>$</span><span>ID</span><span>)</span><span> </span><span># names of map polygons
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>ta_data</span><span>$</span><span>short_name</span><span>)</span><span> </span><span># names of TAs with census data
</span><span>expect_equal</span><span>(</span><span>a</span><span>[</span><span>!</span><span>a</span><span> </span><span>%in%</span><span> </span><span>b</span><span>],</span><span> </span><span>b</span><span>[</span><span>!</span><span>b</span><span> </span><span>%in%</span><span> </span><span>a</span><span>])</span><span>
</span><span># draw graphic:
</span><span>lmplot</span><span>(</span><span>stat.data</span><span> </span><span>=</span><span> </span><span>ta_data</span><span>,</span><span>
</span><span>map.data</span><span> </span><span>=</span><span> </span><span>ta_polys2</span><span>,</span><span>
</span><span>panel.types</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'labels'</span><span>,</span><span> </span><span>'dot'</span><span>,</span><span> </span><span>'dot'</span><span>,</span><span>'map'</span><span>),</span><span>
</span><span>panel.data</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>'short_name'</span><span>,</span><span>'MedianIncome2013'</span><span>,</span><span>'PercNoReligion'</span><span>,</span><span> </span><span>NA</span><span>),</span><span>
</span><span>ord.by</span><span> </span><span>=</span><span> </span><span>'MedianIncome2013'</span><span>,</span><span> </span><span>grouping</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span>
</span><span>median.row</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span># how to merge the two data frames:
</span><span> </span><span>map.link</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'short_name'</span><span>,</span><span> </span><span>'ID'</span><span>),</span><span>
</span><span># stroke colour of the borders of previously-drawn areas:
</span><span> </span><span>map.color2</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span>
</span><span># how to save the result:
</span><span> </span><span>print.file</span><span> </span><span>=</span><span> </span><span>&quot;0095-eg2.png&quot;</span><span>,</span><span>
</span><span>print.res</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span>
</span><span>plot.width</span><span> </span><span>=</span><span> </span><span>14</span><span>,</span><span>
</span><span>plot.height</span><span> </span><span>=</span><span> </span><span>18</span><span>,</span><span>
</span><span># attributes of the panels:
</span><span> </span><span>panel.att</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>list</span><span>(</span><span>1</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Territorial Authorities&quot;</span><span>,</span><span> </span><span>panel.width</span><span> </span><span>=</span><span> </span><span>1.2</span><span>,</span><span> </span><span>panel.header.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>text.size</span><span> </span><span>=</span><span> </span><span>1.6</span><span>,</span><span> </span><span>align</span><span> </span><span>=</span><span> </span><span>'right'</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>text.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>left.margin</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>right.margin</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>xaxis.title.size</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span>
</span><span>list</span><span>(</span><span>2</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Median\nincome&quot;</span><span>,</span><span> </span><span>panel.header.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>xaxis.title</span><span> </span><span>=</span><span> </span><span>'Dollars'</span><span>,</span><span>
</span><span>xaxis.title.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>xaxis.labels.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>graph.bgcolor</span><span> </span><span>=</span><span> </span><span>'grey80'</span><span>,</span><span>
</span><span>graph.grid.color</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span>
</span><span>graph.border.color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>point.border</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>point.size</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span>
</span><span>list</span><span>(</span><span>3</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Percent individuals\nwith no religion&quot;</span><span>,</span><span> </span><span>panel.header.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>xaxis.title</span><span> </span><span>=</span><span> </span><span>'Percent'</span><span>,</span><span>
</span><span>xaxis.title.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>xaxis.labels.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>graph.bgcolor</span><span> </span><span>=</span><span> </span><span>'grey80'</span><span>,</span><span>
</span><span>graph.grid.color</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span>
</span><span>graph.border.color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>point.border</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>point.size</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span>
</span><span>list</span><span>(</span><span>4</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>'\n'</span><span>,</span><span> </span><span>panel.header.size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>panel.width</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;italic&quot;</span><span>,</span><span>
</span><span>active.border.size</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>withdata.border.size</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span>
</span><span>nodata.border.size</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span>
</span><span>))</span></code></pre>
</figure>
<p>Now, there’s a standard way of showing two variables against each other. We lose the spatial element, but for most purposes I think the good old scatter plot is better for this data:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0095-scatter.svg"></p>
<p>…drawn with:</p>
<figure>
<pre><code data-lang="r"><span>ta_data</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>MedianIncome2013</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>PropNoReligion2013</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>short_name</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;Median income&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>dollar</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;No religion&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;lm&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>CensusNightPop2013</span><span>),</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>19</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey60&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>CensusNightPop2013</span><span>),</span><span> </span><span>shape</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;black&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text_repel</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_size_area</span><span>(</span><span>&quot;Census night population&quot;</span><span>,</span><span> </span><span>max_size</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Statistics New Zealand, census 2013&quot;</span><span>)</span></code></pre>
</figure>
<h2>Maybe better with a smaller number of areas?</h2>
<p>New Zealand has 66 districts and cities (not counting Chatham Islands), but only 16 Regional Councils. Perhaps the method works better with a smaller number of areas to show:</p>
<p><img src="https://i1.wp.com/ellisp.github.io/img/0095-eg3.png?w=100%25" data-recalc-dims="1"></p>
<p>… and I think that probably is ok. But really, all we are showing here is 32 numbers. It’s an expensive graphic for something that could almost be as meaningfully shown in a table. All up, my reaction to linked micromaps is one of caution. Like any visualisation tool, I think they’ll be good in some circumstances, but in others they just won’t seem to easily communicate.</p>
<p>Code for the regional council micromap:</p>
<figure>
<pre><code data-lang="r"><span>#================regions example============
# convert from SpatialPolygonsDataFrame into a data frame (similar to fortify)
</span><span>data</span><span>(</span><span>region_simpl</span><span>)</span><span> </span><span># map from mbiemaps
</span><span>reg_polys</span><span> </span><span>&lt;-</span><span> </span><span>create_map_table</span><span>(</span><span>region_simpl</span><span>,</span><span> </span><span>'NAME'</span><span>)</span><span>
</span><span>reg_data</span><span> </span><span>&lt;-</span><span> </span><span>REGC2013</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>PercNoReligion</span><span> </span><span>=</span><span> </span><span>PropNoReligion2013</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>REGC2013_N</span><span> </span><span>!=</span><span> </span><span>&quot;Area Outside Region&quot;</span><span>)</span><span>
</span><span>lmplot</span><span>(</span><span>stat.data</span><span> </span><span>=</span><span> </span><span>reg_data</span><span>,</span><span>
</span><span>map.data</span><span> </span><span>=</span><span> </span><span>reg_polys</span><span>,</span><span>
</span><span>panel.types</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'labels'</span><span>,</span><span> </span><span>'dot'</span><span>,</span><span> </span><span>'dot'</span><span>,</span><span>'map'</span><span>),</span><span>
</span><span>panel.data</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>'REGC2013_N'</span><span>,</span><span>'MedianIncome2013'</span><span>,</span><span>'PercNoReligion'</span><span>,</span><span> </span><span>NA</span><span>),</span><span>
</span><span>ord.by</span><span> </span><span>=</span><span> </span><span>'MedianIncome2013'</span><span>,</span><span> </span><span>grouping</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span>
</span><span>median.row</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span># how to merge the two data frames:
</span><span> </span><span>map.link</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'REGC2013_N'</span><span>,</span><span> </span><span>'ID'</span><span>),</span><span>
</span><span># stroke colour of the borders of previously-drawn areas:
</span><span> </span><span>map.color2</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span>
</span><span># how to save the result:
</span><span> </span><span>print.file</span><span> </span><span>=</span><span> </span><span>&quot;0095-eg3.png&quot;</span><span>,</span><span>
</span><span>print.res</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span>
</span><span>plot.width</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span>
</span><span>plot.height</span><span> </span><span>=</span><span> </span><span>9</span><span>,</span><span>
</span><span># attributes of the panels:
</span><span> </span><span>panel.att</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>list</span><span>(</span><span>1</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Territorial Authorities&quot;</span><span>,</span><span> </span><span>panel.width</span><span> </span><span>=</span><span> </span><span>1.2</span><span>,</span><span> </span><span>text.size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>align</span><span> </span><span>=</span><span> </span><span>'right'</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>text.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>left.margin</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>right.margin</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>list</span><span>(</span><span>2</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Median\nincome&quot;</span><span>,</span><span> </span><span>xaxis.title</span><span> </span><span>=</span><span> </span><span>'Dollars'</span><span>,</span><span>
</span><span>panel.width</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span>
</span><span>left.margin</span><span> </span><span>=</span><span> </span><span>.5</span><span>,</span><span> </span><span>right.margin</span><span> </span><span>=</span><span> </span><span>.5</span><span>,</span><span> </span><span>graph.bgcolor</span><span> </span><span>=</span><span> </span><span>'grey90'</span><span>,</span><span>
</span><span>graph.grid.color</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span>
</span><span>graph.border.color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>point.border</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>point.size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>),</span><span>
</span><span>list</span><span>(</span><span>3</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>&quot;Percent individuals\nwith no religion&quot;</span><span>,</span><span> </span><span>panel.width</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span>
</span><span>left.margin</span><span> </span><span>=</span><span> </span><span>.5</span><span>,</span><span> </span><span>right.margin</span><span> </span><span>=</span><span> </span><span>.5</span><span>,</span><span> </span><span>xaxis.title</span><span> </span><span>=</span><span> </span><span>'Percent'</span><span>,</span><span>
</span><span>graph.bgcolor</span><span> </span><span>=</span><span> </span><span>'grey90'</span><span>,</span><span>
</span><span>graph.grid.color</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span>
</span><span>graph.border.color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span>
</span><span>point.border</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>point.size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>),</span><span>
</span><span>list</span><span>(</span><span>4</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>'\n'</span><span>,</span><span> </span><span>panel.width</span><span> </span><span>=</span><span> </span><span>1.2</span><span>,</span><span>
</span><span>panel.header.font</span><span> </span><span>=</span><span> </span><span>the_font</span><span>,</span><span>
</span><span>panel.header.face</span><span> </span><span>=</span><span> </span><span>&quot;italic&quot;</span><span>,</span><span>
</span><span>active.border.size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>withdata.border.size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>nodata.border.size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>))</span></code></pre>
</figure>
<h2>Bonus – area unit cartograms</h2>
<p>Finally, an aside. My last two blog posts have been about what you might call population-weighted carto-choro-pletho-grams… I’ve been gradually enhancing this <a target="_blank" rel="nofollow" href="https://ellisp.shinyapps.io/nzcensus-cartograms/">Shiny web app which lets people play around with visualisations of 2013 census data</a>. Latest addition is data at the detailed area unit level. Here’s a screenshot showing which area units have higher proportions of people with a background from the Pacific islands:</p>
<p><img src="https://i2.wp.com/ellisp.github.io/img/0094b-pacific-northisland.png?w=100%25" data-recalc-dims="1"></p>
<p>Check out the <a target="_blank" rel="nofollow" href="https://ellisp.shinyapps.io/nzcensus-cartograms/">full app</a> for more.</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="http://ellisp.github.io/blog/2017/04/30/micromaps"> 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/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/oEVKL3EWNgY"></a></div>