<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/minimalist-tufte-inspired-axis-text-with-scottish-new-zealand-historical-material/"></div></div><div><a data-url="http://www.r-bloggers.com/minimalist-tufte-inspired-axis-text-with-scottish-new-zealand-historical-material/" data-counturl="http://www.r-bloggers.com/minimalist-tufte-inspired-axis-text-with-scottish-new-zealand-historical-material/" data-count="horizontal" data-text="Minimalist Tufte-inspired axis text with Scottish-New Zealand historical material" 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/05/14/scots-female-ratio"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Minimalist axes text and tick marks</h2>
<p>One of the ideas in Edward Tufte’s <a target="_blank" href="http://www.amazon.com/Visual-Display-Quantitative-Information/dp/0961392142"><em>The Visual Display of Quantitative Information</em></a> was to minimise non-data-ink by dropping the regular text labelling values on axis guides, and instead using the axis guides to mark the values of the actual data. For those of you with the book in front of you, I’m thinking of the graphic <em>Current Receipts of Government as a Percentage of Gross Domestic Product, 1970 and 1979</em>, on page 158 of the 2013 edition.</p>
<p>Looking for example data on which to apply this, I picked up a table from Rebecca Lenihan’s <a target="_blank" href="http://www.otago.ac.nz/press/books/otago115810.html"><em>From Alba to Aotearoa</em></a> which I’m reading at the moment. It’s a comprehensive look at what can be known about Scottish immigrants to New Zealand from 1840 to 1920 and includes a fair amount of tabular information that is ideal for presentation with this sort of graphic. Here’s where I got to with visualising one of her tables (Table 3.6 on page 105, for those following along), showing the disproportionately maleness of Scottish immigrants in the nineteenth century even compared to other ethnic groups.<br>
<img alt="plot" src="http://ellisp.github.io/img/0040-females.svg"></p>
<p>The key message from this graphic is that for both Scottish and Irish migrants to New Zealand in the nineteenth century, females were under-represented; but the ratio of females to males was noticeably lower for those of Scottish origin (all the provinces to the left of and above the pale blue diagonal line).</p>
<p>Lenihan’s original table includes a range of additional information that can’t be easily shown in this graphic, so the graphic is very much a complement rather than replacement of the detailed numeric information. But for the two dimensions that it does show, it’s nice example of integration of detailed numeric data into spatial representation – a combination of numbers and graphic visualisation.</p>
<h2>Features of this approach</h2>
<p>Now I’ve noticed the trick of over-riding the boring, traditional regular spacing of axis text and tick marks, I think it might become a regular part of the repertoire for sparse two-dimensional data. The key technical features of this as a graphic include:</p>
<ul>
<li>The province labels are carefully in pale grey, and the points representing the actual data are the final layer and in black so they are in the foreground of the graphic</li>
<li>I use pale blue, with different shades of transparency, to put some of the non-data guides (like the line representing equality between the two ethnicities’ female-male ratios) into the background.</li>
<li>Even with this sparse summarised data, some of the actual values are too close for the axis text to include every value. In those cases (eg 81.3% and 81.5% female-male ratios for Canterbury and Otago people of Scottish descent) I’ve included a single average value as the axis text and tick mark, but used “rug” marks to indicate the two different actual values</li>
<li>the vertical and horizontal coordinate systems are forced to be equal, so a centimetre of distance in either direction equates to the same amount of change in female-male ratio</li>
<li>Because it’s all a homage to Tufte, I’ve used serif fonts rather than my usually preferred non-serifs…</li>
</ul>
<h2>Using the breaks= argument in scale_XXX_continuous()</h2>
<p>Here’s the code in R that does all that. Big thanks to the authors of the <code>ggplot2</code>, <code>scales</code>, <code>ggrepel</code> and <code>ggthemes</code> R packages, and a note that the latest dev version (from GitHub) of <code>ggplot2</code> is needed for the subtitle and caption to work.</p>
<div>
<pre><code data-lang="r"><a name="True-1"></a><span>library</span><span>(</span>ggplot2<span>)</span>
<a name="True-2"></a><span>library</span><span>(</span>scales<span>)</span>
<a name="True-3"></a><span>library</span><span>(</span>ggrepel<span>)</span>
<a name="True-4"></a><span>library</span><span>(</span>ggthemes<span>)</span>
<a name="True-5"></a>
<a name="True-6"></a><span># Ratio of females to males in the Scotland- and Ireland- born population in 1881</span>
<a name="True-7"></a>fem_ratio <span>&lt;-</span> <span>data.frame</span><span>(</span>
<a name="True-8"></a>   province <span>=</span> <span>c</span><span>(</span><span>&quot;Auckland&quot;</span><span>,</span> <span>&quot;Taranaki&quot;</span><span>,</span> <span>&quot;Hawke's Bay&quot;</span><span>,</span> <span>&quot;Wellington&quot;</span><span>,</span> <span>&quot;Marlborough&quot;</span><span>,</span> <span>&quot;Nelson&quot;</span><span>,</span> <span>&quot;West Coast&quot;</span><span>,</span> <span>&quot;Canterbury&quot;</span><span>,</span> <span>&quot;Otago&quot;</span><span>),</span> 
<a name="True-9"></a>   scots <span>=</span> <span>c</span><span>(</span><span>68.14</span><span>,</span> <span>55.24</span><span>,</span> <span>58.74</span><span>,</span> <span>71.07</span><span>,</span> <span>51.56</span><span>,</span> <span>52.66</span><span>,</span> <span>38.95</span><span>,</span> <span>67.80</span><span>,</span> <span>75.16</span><span>)</span> <span>/</span> <span>100</span><span>,</span>
<a name="True-10"></a>   irish <span>=</span> <span>c</span><span>(</span><span>84.96</span><span>,</span> <span>43.40</span><span>,</span> <span>65.87</span><span>,</span> <span>87.73</span><span>,</span> <span>69.98</span><span>,</span> <span>50.20</span><span>,</span> <span>67.92</span><span>,</span> <span>81.28</span><span>,</span> <span>81.52</span><span>)</span> <span>/</span> <span>100</span>
<a name="True-11"></a><span>)</span>
<a name="True-12"></a>
<a name="True-13"></a><span># create a set of axis tick/text breakpoints that combine those which are too close</span>
<a name="True-14"></a><span># together for showing on screen:</span>
<a name="True-15"></a>sanitise_ticks <span>&lt;-</span> <span>function</span><span>(</span>x<span>,</span> digits <span>=</span> <span>4</span><span>){</span>
<a name="True-16"></a>   ticks <span>&lt;-</span> <span>sort</span><span>(</span><span>unique</span><span>(</span><span>round</span><span>(</span>x<span>,</span> digits <span>=</span> digits<span>)))</span>
<a name="True-17"></a>   <span>for</span><span>(</span>i <span>in</span> <span>2</span><span>:</span><span>length</span><span>(</span>ticks<span>)){</span>
<a name="True-18"></a>      <span>if</span><span>(</span>ticks<span>[</span>i<span>]</span> <span>-</span> ticks<span>[</span>i<span>-1</span><span>]</span> <span>&lt;</span> <span>0.004</span><span>){</span>
<a name="True-19"></a>         ticks<span>[</span>i<span>-1</span><span>]</span> <span>=</span> <span>mean</span><span>(</span>ticks<span>[(</span>i<span>-1</span><span>)</span><span>:</span>i<span>])</span>
<a name="True-20"></a>         ticks<span>[</span>i<span>]</span> <span>&lt;-</span> <span>0</span>
<a name="True-21"></a>      <span>}</span>
<a name="True-22"></a>   <span>}</span>
<a name="True-23"></a>   ticks <span>&lt;-</span> ticks<span>[</span>ticks <span>!=</span> <span>0</span><span>]</span> 
<a name="True-24"></a><span>}</span>
<a name="True-25"></a>
<a name="True-26"></a>yticks <span>&lt;-</span> sanitise_ticks<span>(</span>fem_ratio<span>$</span>irish<span>)</span>
<a name="True-27"></a>xticks <span>&lt;-</span> sanitise_ticks<span>(</span>fem_ratio<span>$</span>scots<span>)</span>
<a name="True-28"></a>
<a name="True-29"></a>
<a name="True-30"></a><span># Define a frequently used colour and draw plot:</span>
<a name="True-31"></a>linecol <span>&lt;-</span> <span>&quot;steelblue&quot;</span>
<a name="True-32"></a>
<a name="True-33"></a>ggplot<span>(</span>fem_ratio<span>,</span> aes<span>(</span>x <span>=</span> scots<span>,</span> y <span>=</span> irish<span>,</span> label <span>=</span> province<span>))</span> <span>+</span>
<a name="True-34"></a>   theme_tufte<span>()</span> <span>+</span>
<a name="True-35"></a>   geom_abline<span>(</span>intercept <span>=</span> <span>0</span><span>,</span> slope <span>=</span> <span>1</span><span>,</span> colour <span>=</span> linecol<span>,</span> alpha <span>=</span> <span>0.5</span><span>)</span> <span>+</span>
<a name="True-36"></a>   geom_point<span>()</span> <span>+</span>
<a name="True-37"></a>   geom_rug<span>(</span>colour <span>=</span> <span>&quot;steelblue&quot;</span><span>)</span> <span>+</span>
<a name="True-38"></a>   geom_text_repel<span>(</span>colour <span>=</span> <span>&quot;grey60&quot;</span><span>,</span> segment.color <span>=</span> <span>NA</span><span>,</span> family <span>=</span> <span>&quot;serif&quot;</span><span>)</span> <span>+</span>
<a name="True-39"></a>   scale_x_continuous<span>(</span>breaks <span>=</span> xticks<span>,</span> label <span>=</span> percent<span>)</span> <span>+</span>
<a name="True-40"></a>   scale_y_continuous<span>(</span>breaks <span>=</span> yticks<span>,</span> label <span>=</span> percent<span>)</span> <span>+</span>
<a name="True-41"></a>   theme<span>(</span>axis.text <span>=</span> element_text<span>(</span>colour <span>=</span> <span>&quot;steelblue&quot;</span><span>),</span>
<a name="True-42"></a>         axis.ticks <span>=</span> element_line<span>(</span>colour <span>=</span> <span>&quot;steelblue&quot;</span><span>))</span> <span>+</span>
<a name="True-43"></a>   theme<span>(</span>axis.text.x <span>=</span> element_text<span>(</span>angle <span>=</span> <span>60</span><span>,</span> hjust <span>=</span> <span>1</span><span>,</span> vjust <span>=</span> <span>0.5</span><span>))</span> <span>+</span>
<a name="True-44"></a>   labs<span>(</span>x <span>=</span> <span>&quot;nScottish bornn&quot;</span><span>,</span> y <span>=</span> <span>&quot;Irish bornn&quot;</span><span>,</span> 
<a name="True-45"></a>        caption <span>=</span> <span>&quot;Source: Census, analysed in Lenihan 'From Alba to Aotearoa'&quot;</span><span>)</span> <span>+</span>
<a name="True-46"></a>   ggtitle<span>(</span><span>&quot;Females as a percentage of males in New Zealand 1881&quot;</span><span>,</span>
<a name="True-47"></a>           subtitle <span>=</span> <span>&quot;Selected places of birth shown&quot;</span><span>)</span> <span>+</span>
<a name="True-48"></a>   annotate<span>(</span><span>&quot;text&quot;</span><span>,</span> x <span>=</span> <span>0.68</span><span>,</span> y <span>=</span> <span>0.60</span><span>,</span> colour <span>=</span> linecol<span>,</span> family <span>=</span> <span>&quot;serif&quot;</span><span>,</span>
<a name="True-49"></a>            label <span>=</span> <span>&quot;Line shows same rationfor Scots- and Irish- born&quot;</span><span>,</span>
<a name="True-50"></a>            alpha <span>=</span> <span>0.5</span><span>)</span> <span>+</span>
<a name="True-51"></a>   coord_equal<span>()</span></code></pre>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/minimalist-tufte-inspired-axis-text-with-scottish-new-zealand-historical-material/"></div></div><div><a data-url="http://www.r-bloggers.com/minimalist-tufte-inspired-axis-text-with-scottish-new-zealand-historical-material/" data-counturl="http://www.r-bloggers.com/minimalist-tufte-inspired-axis-text-with-scottish-new-zealand-historical-material/" data-count="vertical" data-text="Minimalist Tufte-inspired axis text with Scottish-New Zealand historical material" 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/05/14/scots-female-ratio"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/isykzJrPKIQ"></a></div>