Data animation with gganimate and ggplot
February 6, 2016
On one of his great TED talk presentations, the renowned Swedish physician/statistician Dr. Hans Rosling used data animation to demonstrate the world’s life expectancytrends in wealth, health and economics. His presentations were very impressive not only because of his great explanations, but also due to the data animation overtime demonstration that went hand in hand with it.
Few days ago, on my twitter feed, came an R package that adds animation capability to data in in real-time. Naturally, I wanted to test drive and recreate the example. The package is named
gganimate. This package brings life to the awesome, but static, ggplot2 package plots to come to life! There were additional packages that needed to be installed for the animation. The packages were
image on R,
ImageMagic on Ubuntu.
Without further ado, let’s get right to
gganimate. The data for the plot comes from the
gapminder package used by Dr. Rosling’s presentation. The table contains world’s population growth from 1952 to 2007 in 5 years increment. The data contains 1704 observations and 6 variables. The variables include Country, Continent, Year, Life Expectancy, population and GDP per capital.
library(gganimate) library(imager) library(gapminder) library(ggplot2) theme_set(theme_bw()) p1 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) + geom_point() + scale_x_log10() #gg_animate(p1, "output1.gif") #gg_animate(p1)
The gganimate plot
The data animation you see below is gif generated with the gg_animate function together with the imager and Imagemaker packages. Because some browsers may not have the plugin to play the video, I have embed gif instead. Again, this is data in motion for world health from year 1957 to 2007.