<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/heatmaps-of-mortality-rates/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://kieranhealy.org/blog/archives/2018/12/04/heatmaps-of-mortality-rates/"> R on kieranhealy.org</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>As part of the run-up to the release of <a rel="nofollow" href="https://amzn.to/2vfAixM" target="_blank">Data Visualization</a> (out in about ten days! Currently 30% off on Amazon!), I’ve been playing with graphing different kinds of data. One great source of rich time-series data is <a rel="nofollow" href="http://mortality.org/" target="_blank">mortality.org</a>, which hosts a collection of standardized demographic data for a large number of countries. Mortality rates are often interesting to look at as a heatmap, as we get data for a series of ages (e.g., mortality rates) over some time period. If we make a grid with time on the x-axis and age on the y-axis, we can fill in the boxes with a color representing the rate. A side-effect of that sort of representation is that the diagonals track age cohorts from the bottom left to the upper right of the graph, as people age at the rate of one year per year.</p>
<p>Available mortality data for England and Wales runs back to 1841. Here’s sample code for a plot of that for males:</p>
<div>
<pre><code data-lang="r">

p <span>&lt;-</span> ggplot<span>(</span><span>subset</span><span>(</span>britain<span>,</span> age <span></span> <span>101</span><span>),</span> aes<span>(</span>x <span>=</span> year<span>,</span> y <span>=</span> age<span>,</span> fill <span>=</span> ntile<span>(</span>male<span>,</span> <span>100</span><span>)))</span>
p_out <span>&lt;-</span> p <span>+</span> geom_raster<span>()</span> <span>+</span>
    scale_fill_viridis_c<span>(</span>option <span>=</span> <span>&quot;A&quot;</span><span>,</span> direction <span>=</span> <span>-1</span><span>)</span> <span>+</span>
    scale_x_continuous<span>(</span>breaks <span>=</span> <span>seq</span><span>(</span><span>1845</span><span>,</span> <span>2015</span><span>,</span> by <span>=</span> <span>15</span><span>))</span> <span>+</span>
    ylim<span>(</span><span>c</span><span>(</span><span>0</span><span>,</span> <span>100</span><span>))</span> <span>+</span>
    guides<span>(</span>fill <span>=</span> guide_legend<span>(</span>nrow <span>=</span> <span>1</span><span>,</span> title.position <span>=</span> <span>&quot;top&quot;</span><span>,</span> label.position <span>=</span> <span>&quot;bottom&quot;</span><span>))</span> <span>+</span>
    labs<span>(</span>x <span>=</span> <span>&quot;Year&quot;</span><span>,</span> y <span>=</span> <span>&quot;Age&quot;</span><span>,</span> fill <span>=</span> <span>&quot;Male Death Rate Percentile&quot;</span><span>,</span>
         title <span>=</span> <span>&quot;Male Mortality Rates in England and Wales, 1841-2016&quot;</span><span>,</span>
         subtitle <span>=</span> <span>&quot;Binned by percentile&quot;</span><span>,</span>
         caption <span>=</span> <span>&quot;@kjhealy / http://socviz.co. Data: Human Mortality Database.&quot;</span><span>)</span> <span>+</span>
    theme<span>(</span>legend.position <span>=</span> <span>&quot;top&quot;</span><span>,</span>
          legend.title <span>=</span> element_text<span>(</span>size <span>=</span> <span>8</span><span>))</span>

p_out
ggsave<span>(</span><span>&quot;figures/britain_men.png&quot;</span><span>,</span> p_out<span>,</span> height <span>=</span> <span>9</span><span>,</span> width <span>=</span> <span>10</span><span>)</span>
ggsave<span>(</span><span>&quot;figures/britain_men.pdf&quot;</span><span>,</span> p_out<span>,</span> height <span>=</span> <span>9</span><span>,</span> width <span>=</span> <span>10</span><span>)</span>


</code></pre>
</div>
<p>You can look at the full code on <a rel="nofollow" href="https://github.com/kjhealy/lexis_surface" target="_blank">GitHub</a>.</p>
<p>Here’s the figure for both males and females:</p>
<figure>
<img alt="Mortality rates in England and Wales, 1841-2016, binned by percentile." src="https://i2.wp.com/kieranhealy.org/files/misc/britain_mortality_combined_media-01.png?w=456" data-recalc-dims="1"><figcaption>
<p>Mortality rates in England and Wales, 1841-2016, binned by percentile.</p>
</figcaption></figure>
<p>A <a rel="nofollow" href="http://kieranhealy.org/files/misc/britain_mortality_combined_media.pdf" target="_blank">higher resolution PDF</a> is also available.</p>
<p>The devastating impact of the First and Second World Wars is immediately evident in the graph for males. The Influenza epidemic of 1918 is also clearly visible (most obviously in the female graph). Meanwhile other patterns are also clear, most notably declining infant mortality and increasing life expectancy. The remarkable effect of improved health care after WWII is especially striking. It also brings out some residual differences for men as they enter adulthood and are more likely to die than women of the same age, mostly due to their higher rate of death from accidents of various kinds. We can also make out some apparent cohort effects along the diagonals, notably starting with those born in 1918. They remained at a higher risk of death for their whole lives.</p>
<p>The data for France go back even further, and we have a full two centuries from 1816 to 2016. Here’s what that looks like.</p>
<figure>
<img alt="Mortality rates in France, 1816-2016," src="https://i2.wp.com/kieranhealy.org/files/misc/france_mortality_combined_media-01.png?w=456" data-recalc-dims="1"><figcaption>
<p>Mortality rates in France, 1816-2016,</p>
</figcaption></figure>
<p>Again, a <a rel="nofollow" href="http://kieranhealy.org/files/misc/france_mortality_combined_media.pdf" target="_blank">higher resolution PDF</a> is also available. As with the data for England and Wales, you can see the large effects of wars and epidemics, the decline in infant mortality, and the increase in life expectancy (especially after WWII). The French series also seems to show stronger cohort effects, especially in the 19th century. Notably, I think you can make out (slightly different) diagonals for both men and women that would, if we had the data, intersect the x-axis between around 1789 and 1800, suggesting there were cohort mortality effects for being born during the period of the Revolution and the Terror.</p>
<p>The graphs were made using R and ggplot, of course. I quickly stuck them together in Illustrator (which I wouldn’t do normally, but I was using it for other reasons). Note that there are a few decisions lurking in the background about how to represent the data. If you plot the raw mortality rates, what you’ll find is that the wars tend to drown out all the other data in the series, washing out the other trends. There are various ways to approach this, such as logging the rates. In the end I binned the death rates into percentiles as a compromise. But there are other choices one could make here. The palette is the “Magma” variant of the viridis scale—the latter now comes conveniently built-in to the most recent major version of ggplot.</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="https://kieranhealy.org/blog/archives/2018/12/04/heatmaps-of-mortality-rates/"> R on kieranhealy.org</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/CVvVdd8nL9g" height="1"></a></div>