<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/seasonality-of-plagues-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/11/19/plague-seasonality"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Seasonality of plague deaths</h2>
<p>In the course of my ramblings through history, I recently came across Mark R. Welford and Brian H. Bossak <a rel="nofollow" href="http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0008401" target="_blank">Validation of Inverse Seasonal Peak Mortality in Medieval Plagues, Including the Black Death, in Comparison to Modern Yersinia pestis-Variant Diseases</a>. This article is part of an interesting epidemiological literature on the seasonality of medieval and early modern European plagues and the twentieth century outbreaks of plague caused by the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Yersinia_pestis" target="_blank"><em>Yersinia pestis</em> bacterium</a>. <em>Y. pestis</em> is widely credited (if that’s the word) for the 6th century Plague of Justinian, the mid 14th century Black Death, and the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Third_plague_pandemic" target="_blank">Third Pandemic</a> from the mid nineteenth to mid twentieth centuries. The Black Death killed between 75 and 200 million people in Eurasis in only a few years; the Third Pandemic killed at least 12 million. These are important diseases to understand!</p>
<p>In their 2009 article, Welford and Bossak added additional evidence and rigour to the argument that the difference in seasonality of peak deaths between the first two suspected bubonic plague pandemics and the Third Pandemic indicates a material difference in the diseases, whether it be a variation in <em>Y. pestis</em> itself, its mode and timing of transmission, or even the presence of an unknown human-to-human virus. This latter explanation would account for the extraordinary speed of spread of the Medieval Black Death, which I understand is surprising for a rodent-borne bacterial disease. These are deep waters; as a newcomer to the topic there’s too much risk of me getting it wrong if I try to paraphrase, so I’ll leave it there and encourage you to read specialists in the area.</p>
<h2>Plot polishing</h2>
<p>One of Welford and Bossak’s graphics caught my eye. Their original is reproduced at the bottom of this post. It makes the point of the differing seasonalities in the different outbreaks of plague but is pretty cluttered. I set myself the job of improving it as a way of familiarising myself with the issues. My first go differed from there’s only in minimal respects:</p>
<ul>
<li>split the graphic into four facets for each plague agent / era to allow easier comparison</li>
<li>direct labels of the lines rather than relying on a hard-to-parse legend</li>
<li>added a grey ribbon showing a smoothed overall trend, to give a better idea of the “average” seasonality of the particuar era’s outbreak in each facet</li>
<li>removed gridlines</li>
</ul>
<p><img width="100%" src="http://ellisp.github.io/img/0114-log10.svg"></p>
<p>All the R code for today’s post, from download to animation, is collected at the end of the post.</p>
<p>I think the grey ribbon of the smoothed average line is the best addition here. It makes it much clearer that there is an average seasonality in the European (medieval and early modern) plagues not apparent in the Indian and Chinese examples.</p>
<p>I wasn’t particularly happy with the logarithm transform of the vertical axis. While this is a great transformation for showing changes in terms of relative difference (eg growth rates), when we’re showing seasonality like this the natural comparison is of the distance of each point from the bottom. It is natural to interpret the space as proportionate to a number, as though there is no transformation. In effect, the transformation is hiding the extent of the seasonality.</p>
<p>I suspect the main reason for the use of the transform at all was to avoid the high numbers for some outbreaks hiding the variation in the smaller ones. An alternative approach is to allow the vertical axes of some of the facets to use varying scales. This is legitimate so long as any comparison of magnitudes is <em>within</em> each facet, and comparisons <em>across</em> facets are of trends and patterns, not magnitude. That is the case here. Here’s how that looks:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0114-free_y.svg"></p>
<p>I think this is a significant improvement. The concentration of deaths in a few months of each outbreak is now much clearer.</p>
<p>There’s an obvious problem with both the charts so far, which is the clutter of text from the labels. I think they are an improvement on the legend, but they are a bit of mess. This is in fact a classic problem of how to present longitudinal data in what is often termed a “spaghetti plot” for obvious reasons. Generally, such charts only work if we don’t particularly care which line is which.</p>
<p>I attempted to address this problem by aggregating the samples to the country level, while keeping different lines for different combinations of country and year. I made a colour palette matched to countries so they would have the same colour over time. I also changed the scale to being the proportion of deaths in each month from the total year’s outbreak. If what we’re after is in fact the proportion of deaths in each month (ie the seasonality) rather than the magnitudes, then let’s show that on the graphic:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0114-proportions.svg"></p>
<p>This is starting to look better.</p>
<h2>Animating</h2>
<p>While I think it was worth while aggregating the deaths in those small medieval towns and villages to get a less cluttered plot, it does lose some lovely granular detail. Did you know for instance that the medieval Welsh town of Abergwillar is completely invisible on the internet other than in discussion of the plague in 1349? One way around this problem of longitudinal data where we have a time series for many cases, themselves of some interest, is to animate through the cases. This also gave me a chance to use Thomas Pedersen’s handy <a rel="nofollow" href="https://github.com/thomasp85/tweenr" target="_blank">tweenr</a> R package to smooth the transitions between each case study.</p>
<p><img src="https://i2.wp.com/ellisp.github.io/img/0114-plague-deaths.gif?w=100%25" data-recalc-dims="1"></p>
<p>Note how I’ve re-used the country colour palette, and used the extra visual space given by spreading facets out over time (in effect) to add some additional contextual information, like the latitude of each location.</p>
<h2>Some innocent thoughts on the actual topic</h2>
<p>This was a toe in the water for something I’d stumbled across while reading a history book. There’s lots of interesting questions here. Plague is in the news again today. There’s a <a rel="nofollow" href="http://www.who.int/csr/don/15-november-2017-plague-madagascar/en/" target="_blank">big outbreak in Madagascar</a>, with 171 deaths between August and mid November 2017 and more than 2,000 infections. And while I have been writing this blog, the <a rel="nofollow" href="https://www.express.co.uk/news/world/881343/World-war-3-North-korea-plague-Kim-Jong-un" target="_blank">Express has reported on North Korea stockpiling plague bacteria</a> for biological warfare.</p>
<p>On the actual topic of whether seasonality of deaths tells us that the medieval Black Death was different from 19th century Chinese and Indian plagues, I’d like to see more data. For example, what were the death rates by month in China in the 14th century, where the outbreak probably began? Presumably not available.</p>
<p>Nineteenth century Bombay was different from a medieval European village in many important respects. Bombay’s winter in fact is comparable in temperature to Europe’s summer, so the coincidence of these as peak times for plague deaths perhaps has something in common. On the other hand, Manchuria is deadly, freezing cold in its winter when pneumonic plague deaths peaked in our data.</p>
<p>Interesting topic.</p>
<h2>R code</h2>
<p>Here’s the R code that did all that. It’s nothing spectacular. I actually typed by hand the data from the original table, which was only available, as far as I can see, as an image of a table. Any interest is likely to be in the particular niceties of some of the plot polishing eg using a consistent palette for country colour when colour is actually mapped to country-year combination int he data.</p>
<p>Although <code>tweenr</code> is often used in combination with the <code>animate</code> R package, I prefer to do what <code>animate</code> does manually: save the individual frames somewhere I have set myself and call ImageMagick directly via a <code>system()</code> call. I find the <code>animate</code> package adds a layer of problems – particularly on Windows – that sometimes gives me additional headaches. Calling another application via <code>system()</code> works fine.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>openxlsx</span><span>)</span><span>
</span><span>library</span><span>(</span><span>directlabels</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tweenr</span><span>)</span><span>
</span><span>#========download and prepare data=========
</span><span>download.file</span><span>(</span><span>&quot;https://github.com/ellisp/ellisp.github.io/raw/master/data/plague-mortality.xlsx&quot;</span><span>,</span><span>
</span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;plauge-mortality.xlsx&quot;</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span>orig</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>&quot;plague-mortality.xlsx&quot;</span><span>,</span><span> </span><span>sheet</span><span> </span><span>=</span><span> </span><span>&quot;data&quot;</span><span>)</span><span>
</span><span>d</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>orig</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>place</span><span> </span><span>=</span><span> </span><span>Place</span><span>,</span><span> </span><span>year</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>agent</span><span> </span><span>=</span><span> </span><span>Agent</span><span>,</span><span> </span><span>country</span><span> </span><span>=</span><span> </span><span>Country</span><span>,</span><span> </span><span>latitude</span><span> </span><span>=</span><span> </span><span>Latitude</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>month</span><span>,</span><span> </span><span>death_rate</span><span>,</span><span> </span><span>-</span><span>place</span><span>,</span><span> </span><span>-</span><span>year</span><span>,</span><span> </span><span>-</span><span>agent</span><span>,</span><span> </span><span>-</span><span>country</span><span>,</span><span> </span><span>-</span><span>latitude</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># put the months' levels in correct order so they work in charts etc
</span><span> </span><span>mutate</span><span>(</span><span>month</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>month</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Jan&quot;</span><span>,</span><span> </span><span>&quot;Feb&quot;</span><span>,</span><span> </span><span>&quot;Mar&quot;</span><span>,</span><span> </span><span>&quot;Apr&quot;</span><span>,</span><span> </span><span>&quot;May&quot;</span><span>,</span><span> </span><span>&quot;Jun&quot;</span><span>,</span><span>
</span><span>&quot;Jul&quot;</span><span>,</span><span> </span><span>&quot;Aug&quot;</span><span>,</span><span> </span><span>&quot;Sep&quot;</span><span>,</span><span> </span><span>&quot;Oct&quot;</span><span>,</span><span> </span><span>&quot;Nov&quot;</span><span>,</span><span> </span><span>&quot;Dec&quot;</span><span>)),</span><span>
</span><span>month_number</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>month</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>place_date</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>place</span><span>,</span><span> </span><span>year</span><span>),</span><span>
</span><span>agent</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>agent</span><span>,</span><span> </span><span>year</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>place</span><span>,</span><span> </span><span>year</span><span>,</span><span> </span><span>month_number</span><span>)</span><span> </span><span># define a caption for multiple use:
</span><span>the_caption</span><span> </span><span>&lt;-</span><span> </span><span>str_wrap</span><span>(</span><span>&quot;Graphic by Peter Ellis, reworking data and figures published by Mark R. Welford and Brian H. Bossak 'Validation of Inverse Seasonal Peak Mortality in Medieval Plagues, Including the Black Death, in Comparison to Modern Yersinia pestis-Variant Diseases'&quot;</span><span>,</span><span> </span><span>100</span><span>)</span><span>
</span><span>#============faceted version of original graphic=====================
</span><span> </span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>d</span><span>1</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>month_number</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>death_rate</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>place_date</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>aes</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>NULL</span><span>),</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>month_number</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.9</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_log10</span><span>(</span><span>&quot;Number of deaths in a month&quot;</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>1000000</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>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>d</span><span>1</span><span>$</span><span>month</span><span>),</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.5</span><span>,</span><span> </span><span>12</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>agent</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>the_caption</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Seasonal deaths by different plague agents&quot;</span><span>,</span><span>
</span><span>&quot;Medieval and early modern Black Death and plague peaked in summer, whereas early 20th Century outbreaks peaked in late winter&quot;</span><span>)</span><span>
</span><span>direct.label</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>list</span><span>(</span><span>&quot;top.bumpup&quot;</span><span>,</span><span> </span><span>fontfamily</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.8</span><span>))</span><span> </span><span># good
</span><span>
</span><span># version with free y axes as an alternative to the log transform
</span><span>p</span><span>1</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>1</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>agent</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>()</span><span>
</span><span>direct.label</span><span>(</span><span>p</span><span>1</span><span>b</span><span>,</span><span> </span><span>list</span><span>(</span><span>&quot;top.bumpup&quot;</span><span>,</span><span> </span><span>fontfamily</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.8</span><span>))</span><span>
</span><span>#==============================y axis as proportion===================
</span><span>d</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>d</span><span>1</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>country</span><span>,</span><span> </span><span>year</span><span>,</span><span> </span><span>month_number</span><span>,</span><span> </span><span>agent</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>deaths</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>death_rate</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>country_date</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>country</span><span>,</span><span> </span><span>year</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>country_date</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>prop_deaths</span><span> </span><span>=</span><span> </span><span>deaths</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>deaths</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># convert the zeroes back to NA to be consistent with original presentation:
</span><span> </span><span>mutate</span><span>(</span><span>prop_deaths</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>prop_deaths</span><span> </span><span>==</span><span> </span><span>0</span><span>,</span><span> </span><span>NA</span><span>,</span><span> </span><span>prop_deaths</span><span>))</span><span>
</span><span># Defining a palette. This is a little complex because although colour
# is going to be mapped to country_year, I want each country to have its own
# colour regardless of the year. I'm going to use Set1 of Cynthia Brewer's colours,
# except for the yellow which is invisible against a white background
</span><span>pal1</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>country</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>d</span><span>2</span><span>$</span><span>country</span><span>))</span><span>
</span><span>pal1</span><span>$</span><span>colour</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>nrow</span><span>(</span><span>pal1</span><span>)</span><span> </span><span>+</span><span> </span><span>1</span><span>,</span><span> </span><span>&quot;Set1&quot;</span><span>)[</span><span>-6</span><span>]</span><span>
</span><span>pal2</span><span> </span><span>&lt;-</span><span> </span><span>distinct</span><span>(</span><span>d</span><span>2</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>country_date</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>pal1</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;country&quot;</span><span>)</span><span>
</span><span>pal3</span><span> </span><span>&lt;-</span><span> </span><span>pal2</span><span>$</span><span>colour</span><span>
</span><span>names</span><span>(</span><span>pal3</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>pal2</span><span>$</span><span>country_date</span><span>
</span><span># draw graphic with colour mapped to country-year combination, but colours come out
# at country level:
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>d</span><span>2</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>month_number</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>prop_deaths</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>country_date</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>aes</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>NULL</span><span>),</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>+</span><span>
</span><span># geom_point() +
</span><span> </span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>month_number</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_color_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>pal3</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Proportion of the year's total deaths in each month\n&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>0.65</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>d</span><span>1</span><span>$</span><span>month</span><span>),</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.5</span><span>,</span><span> </span><span>12</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>agent</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Seasonal deaths by different plague agents&quot;</span><span>,</span><span>
</span><span>&quot;Medieval and early modern Black Death and plague peaked in summer, whereas early 20th Century outbreaks peaked in late winter&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>the_caption</span><span>)</span><span>
</span><span>direct.label</span><span>(</span><span>p</span><span>2</span><span>,</span><span> </span><span>list</span><span>(</span><span>&quot;top.bumpup&quot;</span><span>,</span><span> </span><span>fontfamily</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.8</span><span>))</span><span>
</span><span>#============animation===================
</span><span>
</span><span>d</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>d</span><span>1</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>place_date</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>latitude</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>prop_deaths</span><span> </span><span>=</span><span> </span><span>death_rate</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>death_rate</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span>
</span><span>prop_deaths</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>prop_deaths</span><span>),</span><span> </span><span>0</span><span>,</span><span> </span><span>prop_deaths</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>place_date</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>place_date</span><span>,</span><span> </span><span>year</span><span>),</span><span>
</span><span>place_date_id</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>place_date</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>place_date_id</span><span>,</span><span> </span><span>month_number</span><span>)</span><span>
</span><span>place_dates</span><span> </span><span>&lt;-</span><span> </span><span>levels</span><span>(</span><span>d</span><span>3</span><span>$</span><span>place_date</span><span>)</span><span>
</span><span># change this to a list of data.frames, one for each state
</span><span>d</span><span>3</span><span>_</span><span>l</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>place_dates</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>d</span><span>3</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>place_date</span><span> </span><span>==</span><span> </span><span>x</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>prop_deaths</span><span>,</span><span> </span><span>month</span><span>,</span><span> </span><span>place_date</span><span>,</span><span> </span><span>agent</span><span>)</span><span>
</span><span>})</span><span>
</span><span># use tweenr to create a single data frame combining those 20+ frames in the list, with interpolated
# values for smooth animation:
</span><span>d</span><span>3</span><span>_</span><span>t</span><span> </span><span>&lt;-</span><span> </span><span>tween_states</span><span>(</span><span>d</span><span>3</span><span>_</span><span>l</span><span>,</span><span> </span><span>tweenlength</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>statelength</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span> </span><span>ease</span><span> </span><span>=</span><span> </span><span>&quot;cubic-in-out&quot;</span><span>,</span><span> </span><span>nframes</span><span> </span><span>=</span><span> </span><span>600</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># caution - tween_states loses the information ont the ordering of factors
</span><span> </span><span>mutate</span><span>(</span><span>month</span><span>=</span><span> </span><span>factor</span><span>(</span><span>as.character</span><span>(</span><span>month</span><span>),</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>d</span><span>1</span><span>$</span><span>month</span><span>)),</span><span>
</span><span>place_date</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>as.character</span><span>(</span><span>place_date</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>place_dates</span><span>)))</span><span>
</span><span># make a temporary folder to store some thousands of PNG files as individual frames of the animation
</span><span>unlink</span><span>(</span><span>&quot;0114-tmp&quot;</span><span>,</span><span> </span><span>recursive</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>dir.create</span><span>(</span><span>&quot;0114-tmp&quot;</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>max</span><span>(</span><span>d</span><span>3</span><span>_</span><span>t</span><span>$</span><span>.frame</span><span>)){</span><span>
</span><span>png</span><span>(</span><span>paste0</span><span>(</span><span>&quot;0114-tmp/&quot;</span><span>,</span><span> </span><span>i</span><span> </span><span>+</span><span> </span><span>10000</span><span>,</span><span> </span><span>&quot;.png&quot;</span><span>),</span><span> </span><span>2000</span><span>,</span><span> </span><span>1000</span><span>,</span><span> </span><span>res</span><span> </span><span>=</span><span> </span><span>200</span><span>)</span><span> </span><span>the_data</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>d</span><span>3</span><span>_</span><span>t</span><span>,</span><span> </span><span>.frame</span><span> </span><span>==</span><span> </span><span>i</span><span>)</span><span>
</span><span>the_title</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>&quot;Deaths from&quot;</span><span>,</span><span> </span><span>the_data</span><span>[</span><span>1</span><span>,</span><span> </span><span>&quot;agent&quot;</span><span>],</span><span> </span><span>&quot;in&quot;</span><span>,</span><span> </span><span>the_data</span><span>[</span><span>1</span><span>,</span><span> </span><span>&quot;place_date&quot;</span><span>])</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>d</span><span>3</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>place_date</span><span> </span><span>==</span><span> </span><span>the_data</span><span>[</span><span>1</span><span>,</span><span> </span><span>&quot;place_date&quot;</span><span>])</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>place_date</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>latitude</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>()</span><span>
</span><span>the_xlab</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>tmp</span><span>,</span><span> </span><span>paste0</span><span>(</span><span>place_date</span><span>,</span><span> </span><span>&quot;, &quot;</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>&quot;, &quot;</span><span>,</span><span> </span><span>latitude</span><span>,</span><span> </span><span>&quot; degrees North&quot;</span><span>))</span><span>
</span><span>the_high_point</span><span> </span><span>&lt;-</span><span> </span><span>the_data</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>prop_deaths</span><span> </span><span>==</span><span> </span><span>max</span><span>(</span><span>prop_deaths</span><span>)</span><span> </span><span>)</span><span> </span><span>%>%</span><span>
</span><span>slice</span><span>(</span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>prop_deaths</span><span> </span><span>=</span><span> </span><span>prop_deaths</span><span> </span><span>+</span><span> </span><span>0.03</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>lab</span><span> </span><span>=</span><span> </span><span>agent</span><span>)</span><span>
</span><span>the_colour</span><span> </span><span>&lt;-</span><span> </span><span>pal2</span><span>[</span><span>pal2</span><span>$</span><span>country</span><span> </span><span>==</span><span> </span><span>tmp</span><span>$</span><span>country</span><span>,</span><span> </span><span>]</span><span>$</span><span>colour</span><span>[</span><span>1</span><span>]</span><span>
</span><span>print</span><span>(</span><span>
</span><span>ggplot</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>month</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>prop_deaths</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>aes</span><span>(</span><span>ymax</span><span> </span><span>=</span><span> </span><span>prop_deaths</span><span>),</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>the_colour</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.1</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>the_xlab</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>d</span><span>1</span><span>$</span><span>month</span><span>),</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.5</span><span>,</span><span> </span><span>12</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>the_title</span><span>,</span><span> </span><span>&quot;Distribution by month of the total deaths that year&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>the_high_point</span><span>,</span><span> </span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>lab</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>the_caption</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Proportion of the year's total deaths in each month\n&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>0.68</span><span>))</span><span>
</span><span>)</span><span>
</span><span>dev.off</span><span>()</span><span>
</span><span># counter:
</span><span> </span><span>if</span><span> </span><span>(</span><span>i</span><span> </span><span>/</span><span> </span><span>20</span><span> </span><span>==</span><span> </span><span>round</span><span>(</span><span>i</span><span> </span><span>/</span><span> </span><span>20</span><span>)){</span><span>cat</span><span>(</span><span>i</span><span>)}</span><span>
</span><span>}</span><span>
</span><span># combine all those frames into a single animated gif
</span><span>pd</span><span> </span><span>&lt;-</span><span> </span><span>setwd</span><span>(</span><span>&quot;0114-tmp&quot;</span><span>)</span><span>
</span><span>system</span><span>(</span><span>'magick -loop 0 -delay 8 *.png &quot;0114-plague-deaths.gif&quot;'</span><span>)</span><span>
</span><span>setwd</span><span>(</span><span>pd</span><span>)</span><span>
</span><span># clean up
</span><span>unlink</span><span>(</span><span>&quot;plague-mortality.xlsx&quot;</span><span>)</span></code></pre>
</figure>
<h2>The original</h2>
<p>As promised, here’s the original Welford and Bossak image. Sadly, they dropped Abergwillar; and they have data for Poona which I couldn’t find in a table in their paper.</p>
<p><img src="https://i1.wp.com/ellisp.github.io/img/0114-original.png?w=100%25" data-recalc-dims="1"></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/11/19/plague-seasonality"> 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 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/5EvWIiCu_Gs" height="1"></a></div>