<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/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/"></div></div><div><a data-url="http://www.r-bloggers.com/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/" data-counturl="http://www.r-bloggers.com/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/" data-count="horizontal" data-text="Your data vis “Spidey-sense” &amp; the need for a robust “utility belt”" 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://rud.is/b/2016/06/16/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://twitter.com/theboysmithy">@theboysmithy</a> did a <a target="_blank" href="http://www.ft.com/intl/cms/s/0/6f777c84-322b-11e6-ad39-3fee5ffe5b5b.html">great piece</a> on coming up with an alternate view for a timeline for an FT piece. </p>
<p>Here’s an excerpt (read the whole piece, though, it’s worth it):</p>
<div>
<p>Here is an example from a story recently featured in the FT: emerging- market populations are expected to age more rapidly than those in developed countries. The figures alone are compelling: France is expected to take 157 years (from 1865 to 2022) to triple the proportion of its population aged over 65, from 7 per cent to 21 per cent; for China, the equivalent period is likely to be just 34 years (from 2001 to 2035).</p>
<p><img src="http://im.ft-static.com/content/images/43a2f7e4-3223-11e6-bda0-04585c31b153.img"></p>
<p>You may think that visualising this story is as simple as creating a bar chart of the durations ordered by length. In fact, we came across just such a chart from a research agency.</p>
<p>But, to me, this approach generates “the feeling” — and further scrutiny reveals specific problems. A reader must work hard to memorise the date information next to the country labels to work out if there is a relationship between the start date and the length of time taken for the population to age. The chart is clearly not ideal, but how do we improve it?</p>
</div>
<p>Alan went on to talk about the process of improving the vis, eventually turning to <a target="_blank" href="https://en.wikipedia.org/wiki/Joseph_Priestley">Joseph Priestly</a> for inspiration. Here’s their makeover:</p>
<p><img alt="" src="http://im.ft-static.com/content/images/4a75d9ce-3223-11e6-bda0-04585c31b153.img"></p>
<p>Alan used D3 to make this, which had me head scratching for a bit. Bostock is genius &amp; I :heart: D3 immensely, but I never really thought of it as a “canvas” for doing general data visualization creation for something like a print publication (it’s geared towards making incredibly data-rich interactive visualizations). It’s 100% cool to do so, though. It has fine-grained control over every aspect of a visualization and you can easily turn SVGs into PDFs or use them in programs like Illustrator to make the final enhancements. However, D3 is not the only tool that can make a chart like this.</p>
<p>I made the following in R (of course):</p>
<p><a target="_blank" href="https://rud.is/b/wp-content/uploads/2016/06/RStudio.png"><img alt="RStudio" width="960" class="wp-image-4454" src="https://rud.is/b/wp-content/uploads/2016/06/RStudio.png"></a></p>
<p>The annotations in Alan’s image were (99% most likely) made with something like Illustrator. I stopped short of fully reproducing the image (life is super-crazy, still), but could have done so (the entire image is one <code>ggplot2</code> object). </p>
<p>This isn’t an “R > D3” post, though, since I use both. It’s about (a) reinforcing Alan’s posits that we should absolutely take inspiration from historical vis pioneers (so read more!) + need a diverse visualization “utility belt” (ref: Batman) to ensure you have the necessary tools to make a given visualization; (b) trusting your “Spidey-sense” when it comes to evaluating your creations/decisions; and, (c) showing that R is a great alternative to D3 for something like this <img alt=":-)" class="wp-smiley" src="http://i2.wp.com/www.r-bloggers.com/wp-includes/images/smilies/simple-smile.png?w=456" data-recalc-dims="1"></p>
<p>Spider-man (you expected headier references from a dude with a shield avatar?) has this ability to sense danger right before it happens and if you’re making an effort to develop and share great visualizations, you definitely have this same sense in your DNA (though I would not recommend tossing pie charts at super-villains to stop them). When you’ve made something and it just doesn’t “feel right”, look to other sources of inspiration or reach out to your colleagues or the community for ideas or guidance. You <em>can</em> and <em>do</em> make awesome things, and you <em>do</em> have a “Spidey-sense”. You just need to listen to it more, add depth and breadth to your “utility belt” and keep improving with each creation you release into the wild.</p>
<p>R code for the ggplot vis reproduction is below, and it + the CSV file referenced are in <a target="_blank" href="https://gist.github.com/hrbrmstr/f12101f8ef1657f9e1457e0dde1602f8">this gist</a>.</p>
<pre><code>library(ggplot2)
library(dplyr)

ft &lt;- read.csv(&quot;ftpop.csv&quot;, stringsAsFactors=FALSE)

arrange(ft, start_year) %>%
  mutate(country=factor(country, levels=c(&quot; &quot;, rev(country), &quot;  &quot;))) -> ft

ft_labs &lt;- data_frame(
  x=c(1900, 1950, 2000, 2050, 1900, 1950, 2000, 2050),
  y=c(rep(&quot; &quot;, 4), rep(&quot;  &quot;, 4)),
  hj=c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5),
  vj=c(1, 1, 1, 1, 0, 0, 0, 0)
)

ft_lines &lt;- data_frame(x=c(1900, 1950, 2000, 2050))

ft_ticks &lt;- data_frame(x=seq(1860, 2050, 10))

gg &lt;- ggplot()

# tick marks &amp; gridlines
gg &lt;- gg + geom_segment(data=ft_lines, aes(x=x, xend=x, y=2, yend=16),
                        linetype=&quot;dotted&quot;, size=0.15)
gg &lt;- gg + geom_segment(data=ft_ticks, aes(x=x, xend=x, y=16.9, yend=16.6),
                        linetype=&quot;dotted&quot;, size=0.15)
gg &lt;- gg + geom_segment(data=ft_ticks, aes(x=x, xend=x, y=1.1, yend=1.4),
                        linetype=&quot;dotted&quot;, size=0.15)

# double &amp; triple bars
gg &lt;- gg + geom_segment(data=ft, size=5, color=&quot;#b0657b&quot;,
                        aes(x=start_year, xend=start_year+double, y=country, yend=country))
gg &lt;- gg + geom_segment(data=ft, size=5, color=&quot;#eb9c9d&quot;,
                        aes(x=start_year+double, xend=start_year+double+triple, y=country, yend=country))

# tick labels
gg &lt;- gg + geom_text(data=ft_labs, aes(x, y, label=x, hjust=hj, vjust=vj), size=3)

# annotations
gg &lt;- gg + geom_label(data=data.frame(), hjust=0, label.size=0, size=3,
                      aes(x=1911, y=7.5, label=&quot;France is set to take\n157 years to triple the\nproportion ot its\npopulation aged 65+,\nChina only 34 years&quot;))
gg &lt;- gg + geom_curve(data=data.frame(), aes(x=1911, xend=1865, y=9, yend=15.5),
                      curvature=-0.5, arrow=arrow(length=unit(0.03, &quot;npc&quot;)))
gg &lt;- gg + geom_curve(data=data.frame(), aes(x=1915, xend=2000, y=5.65, yend=5),
                      curvature=0.25, arrow=arrow(length=unit(0.03, &quot;npc&quot;)))

# pretty standard stuff here
gg &lt;- gg + scale_x_continuous(expand=c(0,0), limits=c(1860, 2060))
gg &lt;- gg + scale_y_discrete(drop=FALSE)
gg &lt;- gg + labs(x=NULL, y=NULL, title=&quot;Emerging markets are ageing at a rapid rate&quot;,
                subtitle=&quot;Time taken for population aged 65 and over to double and triple in proportion (from 7% of total population)&quot;,
                caption=&quot;Source: http://on.ft.com/1Ys1W2H&quot;)
gg &lt;- gg + theme_minimal()
gg &lt;- gg + theme(axis.text.x=element_blank())
gg &lt;- gg + theme(panel.grid=element_blank())
gg &lt;- gg + theme(plot.margin=margin(10,10,10,10))
gg &lt;- gg + theme(plot.title=element_text(face=&quot;bold&quot;))
gg &lt;- gg + theme(plot.subtitle=element_text(size=9.5, margin=margin(b=10)))
gg &lt;- gg + theme(plot.caption=element_text(size=7, margin=margin(t=-10)))
gg</code></pre>
<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/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/"></div></div><div><a data-url="http://www.r-bloggers.com/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/" data-counturl="http://www.r-bloggers.com/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/" data-count="vertical" data-text="Your data vis “Spidey-sense” &amp; the need for a robust “utility belt”" 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://rud.is/b/2016/06/16/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/"> R – rud.is</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/_GnSMcELK_o"></a></div>