<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/create-smooth-animations-in-r-with-the-tweenr-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/05/tweenr.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>There are several tools available in R for creating animations (movies) from statistical graphics. The <a target="_blank" rel="nofollow" href="https://mran.microsoft.com/package/animation/">animation package</a> by Yihui Xie will create an animated GIF or video file, using a series of R charts you generate as the frames. And the <a target="_blank" rel="nofollow" href="http://www.ggplot2-exts.org/gganimate.html">gganimate package</a> by David Robinson is an extension to ggplot2 that will create a movie from charts created using the ggplot2 syntax: in much the same way that you can create multiple panels using faceting, you can instead create an animation with multiple frames.</p>
<p>But from a storytelling perspective, such animations can sometimes seem rather disjointed. For example, here's the example (from the <a target="_blank" rel="nofollow" href="http://www.ggplot2-exts.org/gganimate.html">gganimate documentation</a>) of crating an animated bubble chart from the gapminder data. (NOTE: to use the gganimate package, you will need to install <a target="_blank" rel="nofollow" href="https://www.imagemagick.org/">ImageMagick</a>. On <a target="_blank" rel="nofollow" href="https://www.imagemagick.org/script/binary-releases.php#windows">Windows</a>, be sure to select the &quot;install legacy utilities&quot; option during install: gganimate requires the &quot;convert&quot; utility to operate.)</p>
<p><img src="https://i0.wp.com/blog.revolutionanalytics.com/downloads/gapminder-gganimate.gif?w=450" data-recalc-dims="1"></p>
<p>The data are all there, but the presentation lacks the style of the original <a target="_blank" rel="nofollow" href="http://blog.revolutionanalytics.com/2017/02/because-its-friday-remembering-hans-rosling.html">Gapminder presentation</a>, which smoothly moved the bubbles between the known data points separated by five-year intervals. You can achieve a similar effect in R by using the <a target="_blank" rel="nofollow" href="https://github.com/thomasp85/tweenr">tweenr package</a>, available on CRAN. The tweenr package doesn't actually perform any animation itself; rather, it calculates smooth interpolations between data points to create new rows in the data which can be used as frames in the animation created by the gganimate package. Here's the same chart, with data processed by tweenr: </p>
<p><img src="https://i2.wp.com/blog.revolutionanalytics.com/downloads/gapmider-tween.gif?w=450" data-recalc-dims="1"></p>
<p>To achieve this affect, I used the <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/tweenr/versions/0.1.5/topics/tween_elements?tap_a=5644-dce66f&tap_s=10907-287229">tween_elements</a> function to augment the original gapminder data set to make 300 frames of animation, smoothing the transitions of Life Expectancy, GDP per capita, and population, and then animating the result with gganimate. <a target="_blank" rel="nofollow" href="http://paldhous.github.io/ucb/2016/dataviz/week14.html">This snimation tutorial by Peter Aldhous</a> was most helpful in developing the code shown below.</p>
<div>
<pre>library(tweenr)

gapminder_edit <span>&lt;-</span> gapminder <span>%>%</span>
  arrange(country, year) <span>%>%</span>
  select(gdpPercap,lifeExp,year,country, continent, pop) <span>%>%</span>
  rename(x<span>=</span>gdpPercap,y<span>=</span>lifeExp,time<span>=</span>year,id<span>=</span>country) <span>%>%</span>
  mutate(ease<span>=</span><span>&quot;linear&quot;</span>)

gapminder_tween <span>&lt;-</span> tween_elements(gapminder_edit,
                                  <span>&quot;time&quot;</span>, <span>&quot;id&quot;</span>, <span>&quot;ease&quot;</span>, nframes <span>=</span> <span>300</span>) <span>%>%</span>
  mutate(year <span>=</span> round(time), country <span>=</span> .group) <span>%>%</span>
  left_join(gapminder, by<span>=</span>c(<span>&quot;country&quot;</span>,<span>&quot;year&quot;</span>,<span>&quot;continent&quot;</span>)) <span>%>%</span>
  rename(population <span>=</span> pop.x)

p2 <span>&lt;-</span> ggplot(gapminder_tween,
             aes(x<span>=</span>x, y<span>=</span>y, frame <span>=</span> .frame)) <span>+</span>
  geom_point(aes(size<span>=</span>population, color<span>=</span>continent),alpha<span>=</span><span>0.8</span>) <span>+</span>
  xlab(<span>&quot;GDP per capita&quot;</span>) <span>+</span>
  ylab(<span>&quot;Life expectancy at birth&quot;</span>) <span>+</span>
  scale_x_log10(labels<span>=</span>comma)

gganimate(p2, filename<span>=</span><span>&quot;gapminder-tween.gif&quot;</span>, title_frame <span>=</span> <span>FALSE</span>, interval <span>=</span> <span>0.05</span>)
</pre>
</div>
<p>For another nice example using the tweenr package, check out this <a target="_blank" rel="nofollow" href="https://wjakethompson.github.io/post/datasaurus-dozen/">blog post by Jake Thompson</a> replicating the <a target="_blank" rel="nofollow" href="https://www.autodeskresearch.com/publications/samestats">Datasaurus Dozen</a> as an animation in R. (Note that every frame of the animation has almost the exact same set of summary statistics.)</p>
<p><a target="_blank" rel="nofollow" href="https://wjakethompson.github.io/post/datasaurus-dozen/"><img src="https://i2.wp.com/blog.revolutionanalytics.com/downloads/datasaurus.gif?w=450" data-recalc-dims="1"></a></p>
<p>For more on the tweenr package, check out its homepage on Github from its author, Thomas Lin Pedersen.</p>
<p>Github (thomasp85): <a target="_blank" rel="nofollow" href="https://github.com/thomasp85/tweenr">tweenr</a></p>
</div>
<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://blog.revolutionanalytics.com/2017/05/tweenr.html"> Revolutions</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/hD4clybr-LU"></a></div>