<p>With the upcoming holidays, I thought it fitting to finally explore the <a target="_blank" rel="nofollow" href="https://cran.rstudio.com/web/packages/ttbbeer/index.html">ttbbeer</a> package. It contains data on beer ingredients used in US breweries from 2006 to 2015 and on the <a target="_blank" rel="nofollow" href="http://www.stuffyoushouldknow.com/podcasts/sin-taxes.htm">(sin) tax</a> rates for beer, champagne, distilled spirits, wine and various tobacco items since 1862.</p>
<p>I will be exploring these datasets and create animated plots with the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/animation/index.html">animation package</a> and the <a target="_blank" rel="nofollow" href="https://www.ggplot2-exts.org/gganimate.html">gganimate extension for ggplot2</a>.</p>
<p></p>
<p>Merry R-Mas and Cheers! <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<p>(For full code see below…)</p>
<hr>
<p></p>
<h1>Beer materials since 2006</h1>
<p>This dataset includes the monthly aggregates of ingredients as reported by the Beer Monthly Statistical Releases, the Brewer’s Report of Operations and the Quarterly Brewer’s Report of Operations from the US <a target="_blank" rel="nofollow" href="https://www.ttb.gov/">Alcohol and Tobacco Tax and Trade Bureau (TTB)</a>.</p>
<p>The following nine ingredients are listed:</p>
<ul>
<li>Malt and malt products</li>
<li>Corn and corn products</li>
<li>Rice and rice products</li>
<li>Barley and barley products</li>
<li>Wheat and wheat products</li>
<li>Sugar and syrups</li>
<li>Dry hops</li>
<li>Hops extracts</li>
<li>Other ingredients, like flavors, etc.</li>
</ul>
<p>All ingredient amounts are given in pounds.</p>
<p>I’m not only interested in the total amounts of ingredients that were used but also in the change compared to the previous month. This, I’m calculating as the percent of change compared to the previous entry, starting with 0 in January 2006.</p>
<p>The first thing I wanted to know was how many ingredients were used in total each month. This probably gives a rough estimate of how much beer was produced that month.</p>
<p></p>
<p><img src="https://i1.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The seasonal variation in ingredient use shows that more beer is produced in late spring/ early summer than in winter. Given that it takes about one to four months from production to the finished product, it fits my intuition that people drink more beer when it’s warm outside rather than in the cold of winter.</p>
<p>Now lets look at the ingredients separately. This plot shows the monthly sum of pounds for each ingredient:</p>
<p></p>
<p><img src="https://i0.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-7-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We can see the following trends:</p>
<ul>
<li>The seasonal trend in overall beer production is reflected in the individual ingredients</li>
<li>Barley and wheat use has been steadily increasing since 2006</li>
<li>Corn and corn products has spiked between 2008 and 2011 and slightly dropped since</li>
<li>Dry hops has two very prominent spikes in 2014 and 2015, we will look at these in more detail in the following figures</li>
<li>The use of hops extracts, malt and rice has slighty decreased since 2006</li>
<li>Sugar and syrup use has dropped of between 2006 and 2010 and has since been relatively stable, with the exception of two months in 2014 and 2015</li>
<li>Other additives, like flavors, have increased sharply since 2009; in the last two years their use shows extremely high variability between summer (many added) and winter (fewer added); this might be due to the popularity of craft beers and light summer variants.</li>
</ul>
<p>We can see that barley use spikes every third month, except for a few outlier months. Dy hops is interesting as there are two very prominent spikes in December 2014 and in December 2015. In the same months we see a drop for corn, hops extracts, malt, other ingredients, sugar and syrups and wheat.</p>
<p>The following plot shows the percent of change compared to the previous month for the monthly sums of ingredients:</p>
<p></p>
<p><img src="https://i0.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-9-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Here we can again see the seasonality of ingredient use, while we can also see that the overall trend shows no significant change from zero.</p>
<p>The two outlier months, December 2014 and 2015 are quite conspicuous in these plots as well. This might be due to specific trends or there were shortages of supply for fresh hops and malt during these months (even though we don’t see a drop in overall ingredient amounts).</p>
<p>The first animated plot shows the percentages of ingredients that were used per month. Each frame shows one year.</p>
<p></p>
<p><img src="https://i2.wp.com/shiring.github.io/animation/2016/12/22/beer_ingredients.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The percentage plots show that there are no major differences in the proportions of ingredients, again with the exception of December 2014 and 2015.</p>
<hr>
<p></p>
<h1>Sin taxes since 1862</h1>
<p>Tax rates for beer, champagne, distilled spirits, wine (separated into three alcohol percentage classes: up to 14%, 14 – 21% and 21 24%) and various tobacco items are included in separate datasets. The tax rates are given in US Dollar per</p>
<ul>
<li>Barrel of beer (31 gallons)</li>
<li>Wine gallon</li>
<li>Proof barrel of distilled spirits</li>
<li>1000 units of small and large cigarettes and cigars</li>
<li>1 lb. of pipe, chewing and roll-your-own tobacco and snuff</li>
</ul>
<p>Before I can visualize the data, I need to to do some tidying and preprocessing:</p>
<ul>
<li>I’m adding a column with the tax group</li>
<li>to keep it consistent, I’m converting all tobacco item names to upper case letters</li>
<li>to dates in the tobacco set were probably wrong, because they messed up the plots, so I corrected them</li>
</ul>
<p>For each tax group I also want to know the percent of change in tax rate compared to the previous time point.</p>
<p>The first plot shows a time line of tax rate changes since 1862. Major historical events are marked.</p>
<p></p>
<p><img src="https://i1.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-17-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The strongest increase in tax rates can be seen for champagne in the 1950s and for wine (14%) following the end of the Cold War. At the end of the civil war, there is a sharp drop in the tax rates for distilled spirits. Spirit tax increased little between 1874 and the beginning of prohibition. During prohibition we can also see an increase of tax rates for champagne and wine (21-24%). While tax rates remained stable during the Cold War, they increased again in 1991 for all drinks, except for spirits.</p>
<p>The same plot as above but with absolute tax rates is visualized in the animated plot below:</p>
<p></p>
<p><img src="https://i1.wp.com/shiring.github.io/animation/2016/12/22/taxes_by_tax.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<p>The plot below catches the same information in a static plot:</p>
<p></p>
<p><img src="https://i1.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-19-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>All tax rates have increased strongly since 1862. Beer and wine show a strong jump in 1991 and at the beginning of prohibition. Between the end of prohibition and World War II, beer and wine taxes had dropped again, albeit slightly. Since the 1940s, all alcohol taxes have risen steadily. For tobacoo there is only sporadic information.</p>
<hr>
<p></p>
<h1>R code</h1>
<div>
<pre><code><span>library</span><span>(</span><span>ttbbeer</span><span>)</span><span>
</span><span>data</span><span>(</span><span>&quot;beermaterials&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span># order by year from least to most recent
</span><span>beermaterials_change</span><span> </span><span>&lt;-</span><span> </span><span>arrange</span><span>(</span><span>beermaterials</span><span>,</span><span> </span><span>Year</span><span>)</span><span>
</span><span># calculate percent change for each ingredient column
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>3</span><span>:</span><span>ncol</span><span>(</span><span>beermaterials_change</span><span>)){</span><span>
</span><span>value</span><span> </span><span>&lt;-</span><span> </span><span>beermaterials_change</span><span>[[</span><span>colnames</span><span>(</span><span>beermaterials_change</span><span>)[</span><span>i</span><span>]]]</span><span>
</span><span>beermaterials_change</span><span>[,</span><span> </span><span>i</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span> </span><span>*</span><span> </span><span>round</span><span>((</span><span>value</span><span>[</span><span>-1</span><span>]</span><span> </span><span>-</span><span> </span><span>value</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>value</span><span>)</span><span>-1</span><span>)])</span><span> </span><span>/</span><span>
</span><span>value</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>value</span><span>)</span><span>-1</span><span>)],</span><span> </span><span>2</span><span>))</span><span>
</span><span>}</span><span>
</span><span>head</span><span>(</span><span>beermaterials_change</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## # A tibble: 6 × 11
##      Month  Year Malt_and_malt_products `Corn_and _corn_products`
##      &lt;chr> &lt;int>                  &lt;dbl>                     &lt;dbl>
## 1  January  2006                      0                         0
## 2 February  2006                     -8                        -1
## 3    March  2006                     11                         7
## 4    April  2006                      2                        -6
## 5      May  2006                      5                        10
## 6     June  2006                     -2                       -15
## # ... with 7 more variables: Rice_and_rice_products &lt;dbl>,
## #   Barley_and_barley_products &lt;dbl>, Wheat_and_wheat_products &lt;dbl>,
## #   Sugar_and_syrups &lt;dbl>, Hops_dry &lt;dbl>, Hops_extracts &lt;dbl>,
## #   Other &lt;dbl>
</code></pre>
</div>
<div>
<pre><code><span>#calcluate sum of ingredients per month
</span><span>beermaterials</span><span>$</span><span>sum</span><span> </span><span>&lt;-</span><span> </span><span>rowSums</span><span>(</span><span>beermaterials</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>2</span><span>)])</span><span>
</span></code></pre>
</div>
<p>For plotting I’m converting the tables from wide to long format:</p>
<div>
<pre><code><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>beermaterials_gather</span><span> </span><span>&lt;-</span><span> </span><span>beermaterials</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Ingredient</span><span>,</span><span> </span><span>Amount</span><span>,</span><span> </span><span>Malt_and_malt_products</span><span>:</span><span>Other</span><span>)</span><span>
</span><span>beermaterials_gather</span><span>$</span><span>Date</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>&quot;01&quot;</span><span>,</span><span> </span><span>beermaterials_gather</span><span>$</span><span>Month</span><span>,</span><span> </span><span>beermaterials_gather</span><span>$</span><span>Year</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;-&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.Date</span><span>(</span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;%d-%B-%Y&quot;</span><span>)</span><span>
</span><span>beermaterials_gather</span><span>$</span><span>Ingredient</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;_&quot;</span><span>,</span><span> </span><span>&quot; &quot;</span><span>,</span><span> </span><span>beermaterials_gather</span><span>$</span><span>Ingredient</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>beermaterials_change_gather</span><span> </span><span>&lt;-</span><span> </span><span>beermaterials_change</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Ingredient</span><span>,</span><span> </span><span>Amount</span><span>,</span><span> </span><span>Malt_and_malt_products</span><span>:</span><span>Other</span><span>)</span><span>
</span><span>beermaterials_change_gather</span><span>$</span><span>Date</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>&quot;01&quot;</span><span>,</span><span> </span><span>beermaterials_change_gather</span><span>$</span><span>Month</span><span>,</span><span> </span><span>beermaterials_change_gather</span><span>$</span><span>Year</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;-&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.Date</span><span>(</span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;%d-%B-%Y&quot;</span><span>)</span><span>
</span><span>beermaterials_change_gather</span><span>$</span><span>Ingredient</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;_&quot;</span><span>,</span><span> </span><span>&quot; &quot;</span><span>,</span><span> </span><span>beermaterials_change_gather</span><span>$</span><span>Ingredient</span><span>)</span><span>
</span></code></pre>
</div>
<p>And I’m preparing my custom ggplot2 theme:</p>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>my_theme</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;sans&quot;</span><span>){</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>base_size</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>base_family</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;aliceblue&quot;</span><span>),</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;royalblue&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>,</span><span>
</span><span>legend.justification</span><span> </span><span>=</span><span> </span><span>&quot;top&quot;</span><span>,</span><span> </span><span>legend.box</span><span> </span><span>=</span><span> </span><span>&quot;horizontal&quot;</span><span>,</span><span>
</span><span>legend.box.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>),</span><span>
</span><span>legend.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>theme_set</span><span>(</span><span>my_theme</span><span>())</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>beermaterials_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>sum</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>&quot;dashed&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Amount in pounds&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Sum of Beer Ingredients by Month and Year&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Sum of monthly aggregates of all ingredients from 2006 to 2015&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;6 month&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%B %Y&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>beermaterials_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Amount</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>Ingredient</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>&quot;dashed&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>Ingredient</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</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>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Amount in pounds&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Beer Ingredients by Month and Year&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Monthly aggregates of ingredients from 2006 to 2015&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;1 year&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%Y&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>beermaterials_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Amount</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>Ingredient</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Amount in pounds&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Beer Ingredients by Month and Year&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;from January 2006 to December 2015&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;1 month&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%B %Y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>Ingredient</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>To explore the seasonal changes in more detail, I’m widening the resolution of the x-axis.</p>
<p><img src="https://i0.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>ggplot</span><span>(</span><span>beermaterials_change_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Amount</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>Ingredient</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>&quot;dashed&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>Ingredient</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</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>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Percent change&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Beer Ingredients by Month and Year&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Percent change of monthly aggregates of ingredients compared to previous month from 2006 to 2015&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;1 year&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%Y&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>beermaterials_percent</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>beermaterials</span><span>[,</span><span> </span><span>1</span><span>:</span><span>2</span><span>],</span><span> </span><span>prop.table</span><span>(</span><span>as.matrix</span><span>(</span><span>beermaterials</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>2</span><span>)]),</span><span> </span><span>margin</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span>
</span><span>beermaterials_percent_gather</span><span> </span><span>&lt;-</span><span> </span><span>beermaterials_percent</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Ingredient</span><span>,</span><span> </span><span>Amount</span><span>,</span><span> </span><span>Malt_and_malt_products</span><span>:</span><span>Other</span><span>)</span><span>
</span><span>beermaterials_percent_gather</span><span>$</span><span>Ingredient</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;_&quot;</span><span>,</span><span> </span><span>&quot; &quot;</span><span>,</span><span> </span><span>beermaterials_percent_gather</span><span>$</span><span>Ingredient</span><span>)</span><span>
</span><span>f</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>beermaterials_percent_gather</span><span>$</span><span>Month</span><span>)</span><span>
</span><span>beermaterials_percent_gather</span><span> </span><span>&lt;-</span><span> </span><span>within</span><span>(</span><span>beermaterials_percent_gather</span><span>,</span><span> </span><span>Month</span><span> </span><span>&lt;-</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>f</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>animation</span><span>)</span><span>
</span><span>ani.options</span><span>(</span><span>ani.width</span><span>=</span><span>800</span><span>,</span><span> </span><span>ani.height</span><span>=</span><span>500</span><span>)</span><span>
</span><span>saveGIF</span><span>({</span><span>
</span><span>for</span><span> </span><span>(</span><span>year</span><span> </span><span>in</span><span> </span><span>rev</span><span>(</span><span>unique</span><span>(</span><span>beermaterials_percent_gather</span><span>$</span><span>Year</span><span>))){</span><span>
</span><span>pie</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>subset</span><span>(</span><span>beermaterials_percent_gather</span><span>,</span><span> </span><span>Year</span><span> </span><span>==</span><span> </span><span>paste</span><span>(</span><span>year</span><span>)),</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Amount</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Ingredient</span><span>,</span><span> </span><span>frame</span><span> </span><span>=</span><span> </span><span>Year</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>theme_minimal</span><span>()</span><span> </span><span>+</span><span>
</span><span>coord_polar</span><span>(</span><span>&quot;y&quot;</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Percentage of Beer Ingredients by Year and Month&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>year</span><span>))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.title.x</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.title.y</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>plot.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>,</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>),</span><span>
</span><span>legend.title</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>,</span><span>
</span><span>legend.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>8</span><span>)</span><span>
</span><span>)</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>Month</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>6</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>ncol</span><span> </span><span>=</span><span> </span><span>9</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span>
</span><span>print</span><span>(</span><span>pie</span><span>)</span><span>
</span><span>}</span><span>
</span><span>},</span><span> </span><span>movie.name</span><span> </span><span>=</span><span> </span><span>&quot;beer_ingredients.gif&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>beermaterials_percent_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Month</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Amount</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>Ingredient</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Ingredient</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.7</span><span>)</span><span> </span><span>+</span><span> </span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>Year</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Percent&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Percentage of Beer Ingredients by Year and Month&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;from 2006 to 2015&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p>To see the information from the animated figure at one glance, the next plots shows the percentages of ingredients per month and year as a barplot:</p>
<p><img src="https://i0.wp.com/shiring.github.io/animation/2016/12/22/alcohol_post_files/figure-markdown_github/unnamed-chunk-13-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<hr>
<div>
<pre><code><span>beertax</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>&quot;BEER&quot;</span><span>
</span><span>champagnetax</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>&quot;CHAMPAGNE&quot;</span><span>
</span><span>spirittax</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>&quot;SPIRIT&quot;</span><span>
</span><span>tobaccotax</span><span>$</span><span>ITEM</span><span> </span><span>&lt;-</span><span> </span><span>toupper</span><span>(</span><span>tobaccotax</span><span>$</span><span>ITEM</span><span>)</span><span>
</span><span>tobaccotax</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>&quot;TOBACCO&quot;</span><span>,</span><span> </span><span>tobaccotax</span><span>$</span><span>ITEM</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot; &quot;</span><span>)</span><span>
</span><span>tobaccotax</span><span> </span><span>&lt;-</span><span> </span><span>tobaccotax</span><span>[,</span><span> </span><span>-1</span><span>]</span><span>
</span><span>tobaccotax</span><span>[</span><span>18</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;1977-01-31&quot;</span><span>
</span><span>tobaccotax</span><span>[</span><span>18</span><span>,</span><span> </span><span>1</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;1977-01-01&quot;</span><span>
</span><span>winetax14</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>&quot;WINE 14%&quot;</span><span>
</span><span>winetax1421</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>&quot;WINE 14-21%&quot;</span><span>
</span><span>winetax2124</span><span>$</span><span>tax</span><span> </span><span>&lt;-</span><span> </span><span>&quot;WINE 21-24%&quot;</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>taxes_list</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;beertax&quot;</span><span>,</span><span> </span><span>&quot;champagnetax&quot;</span><span>,</span><span> </span><span>&quot;spirittax&quot;</span><span>,</span><span> </span><span>&quot;tobaccotax&quot;</span><span>,</span><span> </span><span>&quot;winetax14&quot;</span><span>,</span><span> </span><span>&quot;winetax1421&quot;</span><span>,</span><span> </span><span>&quot;winetax2124&quot;</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>tax</span><span> </span><span>in</span><span> </span><span>taxes_list</span><span>){</span><span>
</span><span>tax_df</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>tax</span><span>)</span><span>
</span><span>tax_df</span><span> </span><span>&lt;-</span><span> </span><span>tax_df</span><span>[</span><span>match</span><span>(</span><span>sort</span><span>(</span><span>tax_df</span><span>$</span><span>FROM</span><span>),</span><span> </span><span>tax_df</span><span>$</span><span>FROM</span><span>),</span><span> </span><span>]</span><span>
</span><span>if</span><span> </span><span>(</span><span>tax</span><span> </span><span>!=</span><span> </span><span>&quot;tobaccotax&quot;</span><span>){</span><span>
</span><span>tax_df</span><span>$</span><span>change</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span> </span><span>*</span><span> </span><span>round</span><span>((</span><span>tax_df</span><span>$</span><span>RATE</span><span>[</span><span>-1</span><span>]</span><span> </span><span>-</span><span> </span><span>tax_df</span><span>$</span><span>RATE</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>tax_df</span><span>$</span><span>RATE</span><span>)</span><span>-1</span><span>)])</span><span> </span><span>/</span><span>
</span><span>tax_df</span><span>$</span><span>RATE</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>tax_df</span><span>$</span><span>RATE</span><span>)</span><span>-1</span><span>)],</span><span> </span><span>2</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>tax_df</span><span>$</span><span>change</span><span> </span><span>&lt;-</span><span> </span><span>&quot;NA&quot;</span><span>
</span><span>}</span><span>
</span><span>if</span><span> </span><span>(</span><span>tax</span><span> </span><span>==</span><span> </span><span>&quot;beertax&quot;</span><span>){</span><span>
</span><span>tax_df_2</span><span> </span><span>&lt;-</span><span> </span><span>tax_df</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>tax_df_2</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>tax_df_2</span><span>,</span><span> </span><span>tax_df</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>tax_2</span><span> </span><span>&lt;-</span><span> </span><span>tax_df_2</span><span>
</span><span>tax_2</span><span>$</span><span>ID</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>tax_2</span><span>$</span><span>tax</span><span>,</span><span> </span><span>rownames</span><span>(</span><span>tax_2</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>)</span><span>
</span><span>head</span><span>(</span><span>tax_2</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##         FROM         TO RATE  tax change     ID
## 4 1862-09-01 1863-03-03  1.0 BEER      0 BEER_4
## 5 1863-03-03 1864-03-31  0.6 BEER    -40 BEER_5
## 6 1864-04-01 1898-06-13  1.0 BEER     67 BEER_6
## 7 1898-06-14 1901-06-30  2.0 BEER    100 BEER_7
## 8 1901-07-01 1902-06-30  1.6 BEER    -20 BEER_8
## 9 1902-07-01 1914-10-22  1.0 BEER    -38 BEER_9
</code></pre>
</div>
<p>Now I can gather the dataframe for plotting.</p>
<div>
<pre><code><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>tax_gather</span><span> </span><span>&lt;-</span><span> </span><span>tax_2</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>dat_column</span><span>,</span><span> </span><span>Date</span><span>,</span><span> </span><span>FROM</span><span>:</span><span>TO</span><span>)</span><span>
</span><span>tax_gather</span><span>$</span><span>group</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot; .*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>tax_gather</span><span>$</span><span>tax</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>d</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>xmin</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>c</span><span>(</span><span>&quot;1861-04-12&quot;</span><span>,</span><span> </span><span>&quot;1914-07-28&quot;</span><span>,</span><span> </span><span>&quot;1920-01-16&quot;</span><span>,</span><span> </span><span>&quot;1939-11-01&quot;</span><span>,</span><span> </span><span>&quot;1947-03-12&quot;</span><span>)),</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>-</span><span>Inf</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>c</span><span>(</span><span>&quot;1865-04-09&quot;</span><span>,</span><span> </span><span>&quot;1918-11-11&quot;</span><span>,</span><span> </span><span>&quot;1933-12-05&quot;</span><span>,</span><span> </span><span>&quot;1945-05-08&quot;</span><span>,</span><span> </span><span>&quot;1991-12-26&quot;</span><span>)),</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>Inf</span><span>,</span><span> </span><span>description</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Civial War&quot;</span><span>,</span><span> </span><span>&quot;WW1&quot;</span><span>,</span><span> </span><span>&quot;Prohibition&quot;</span><span>,</span><span> </span><span>&quot;WW2&quot;</span><span>,</span><span> </span><span>&quot;Cold War&quot;</span><span>),</span><span>
</span><span>pos_y</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>c</span><span>(</span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;1700&quot;</span><span>,</span><span> </span><span>&quot;1500&quot;</span><span>),</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;1700&quot;</span><span>)))</span><span>
</span><span>d</span><span>$</span><span>pos_x</span><span> </span><span>&lt;-</span><span> </span><span>as.Date</span><span>(</span><span>NA</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>d</span><span>)){</span><span>
</span><span>d</span><span>[</span><span>i</span><span>,</span><span> </span><span>&quot;pos_x&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.Date</span><span>(</span><span>as.character</span><span>(</span><span>median</span><span>(</span><span>c</span><span>(</span><span>d</span><span>$</span><span>xmin</span><span>[</span><span>i</span><span>],</span><span> </span><span>d</span><span>$</span><span>xmax</span><span>[</span><span>i</span><span>]))))</span><span>
</span><span>}</span><span>
</span><span>tax_gather</span><span>$</span><span>group2</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>tax_gather</span><span>$</span><span>group</span><span> </span><span>==</span><span> </span><span>&quot;TOBACCO&quot;</span><span>,</span><span> </span><span>&quot;TOBACCO&quot;</span><span>,</span><span> </span><span>&quot;ALCOHOL&quot;</span><span>)</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_rect</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>aes</span><span>(</span><span>xmin</span><span> </span><span>=</span><span> </span><span>xmin</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>xmax</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>ymin</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>ymax</span><span>),</span><span> </span><span>alpha</span><span>=</span><span>0.2</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_label</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>aes</span><span>(</span><span>pos_x</span><span>,</span><span> </span><span>pos_y</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>description</span><span>),</span><span> </span><span>check_overlap</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>subset</span><span>(</span><span>tax_gather</span><span>,</span><span> </span><span>group2</span><span> </span><span>==</span><span> </span><span>&quot;ALCOHOL&quot;</span><span>),</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>change</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>subset</span><span>(</span><span>tax_gather</span><span>,</span><span> </span><span>group2</span><span> </span><span>==</span><span> </span><span>&quot;ALCOHOL&quot;</span><span>),</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>change</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>group2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</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>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Tax rate change in %&quot;</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Alcohol tax rate changes during history&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Change in percent compared to previous timepoint from 1862 to 2016&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>ncol</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;10 year&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%Y&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>d</span><span>$</span><span>pos_y</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>c</span><span>(</span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;20&quot;</span><span>,</span><span> </span><span>&quot;18&quot;</span><span>),</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;20&quot;</span><span>))</span><span>
</span><span>library</span><span>(</span><span>gganimate</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_rect</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>aes</span><span>(</span><span>xmin</span><span> </span><span>=</span><span> </span><span>xmin</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>xmax</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>ymin</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>ymax</span><span>),</span><span> </span><span>alpha</span><span>=</span><span>0.2</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_label</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>aes</span><span>(</span><span>pos_x</span><span>,</span><span> </span><span>pos_y</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>description</span><span>),</span><span> </span><span>check_overlap</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>tax_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>RATE</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>,</span><span> </span><span>frame</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>tax_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>RATE</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>,</span><span> </span><span>frame</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Tax rate&quot;</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Tax rates of &quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;from 1862 to 2016&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>ncol</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;10 year&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%Y&quot;</span><span>)</span><span>
</span><span>ani.options</span><span>(</span><span>ani.width</span><span>=</span><span>1000</span><span>,</span><span> </span><span>ani.height</span><span>=</span><span>600</span><span>)</span><span>
</span><span>gganimate</span><span>(</span><span>p</span><span>,</span><span> </span><span>filename</span><span> </span><span>=</span><span> </span><span>&quot;taxes_by_tax.mp4&quot;</span><span>,</span><span> </span><span>cumulative</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>gganimate</span><span>(</span><span>p</span><span>,</span><span> </span><span>filename</span><span> </span><span>=</span><span> </span><span>&quot;taxes_by_tax.gif&quot;</span><span>,</span><span> </span><span>cumulative</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_rect</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>aes</span><span>(</span><span>xmin</span><span> </span><span>=</span><span> </span><span>xmin</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>xmax</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>ymin</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>ymax</span><span>),</span><span> </span><span>alpha</span><span>=</span><span>0.2</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>tax_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>RATE</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>&quot;dashed&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>tax_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>RATE</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1.5</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>tax_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>RATE</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>tax</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>group</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</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>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Tax rate&quot;</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Alcohol and tobacco tax rates&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;from 1862 to 2016&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>ncol</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span>byrow</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;20 year&quot;</span><span>,</span><span> </span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%Y&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<hr>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_2.2.0 tidyr_0.6.0   dplyr_0.5.0   ttbbeer_1.1.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.8      knitr_1.15.1     magrittr_1.5     munsell_0.4.3   
##  [5] lattice_0.20-34  colorspace_1.3-2 R6_2.2.0         stringr_1.1.0   
##  [9] plyr_1.8.4       tools_3.3.2      grid_3.3.2       nlme_3.1-128    
## [13] gtable_0.2.0     mgcv_1.8-16      DBI_0.5-1        htmltools_0.3.5 
## [17] yaml_2.1.14      lazyeval_0.2.0   assertthat_0.1   rprojroot_1.1   
## [21] digest_0.6.10    tibble_1.2       Matrix_1.2-7.1   evaluate_0.10   
## [25] rmarkdown_1.2    labeling_0.3     stringi_1.1.2    scales_0.4.1    
## [29] backports_1.0.4
</code></pre>
</div>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/AnMjHSeSXvI">