An Intro to Animating Charts with gganimate

Joe Drigo Enriquez

2020-03-31

Data visualization with R is an essential skill to develop for us to communicate effectively with stakeholders.

Thankfully, packages like ggplot exist which allow us to make impactful charts with a wide flexibility of visualization options.

In this tutorial, we’ll illustrate how to take these ggplot visualizations further by animating them using the gganimate package by Thomas Lin Pedersen. The gganimate pack builds on top of ggplot, allowing us to make compelling data animations with just a few additional lines of code.

We’ll go through two examples in this tutorial:

  1. ChickWeights dataset - an experiment on different poultry diets, tracking the weight of chicks over 21 days

  2. Gapminder dataset - Life expectancy and GDP per capita of Australia and New Zealand from 1952 to 2007

Example 1: Poultry Diets Experiment

Install the gganimate package

First things first - ensure that you have ggplot2 (included in tidyverse) and gganimate packages installed, then call them via the library command:

Plot the static visual

Before we add the gganimate code lines, it’s good practice to first plan and plot out our static visual through ggplot.

For this example we’re using the ChickWeight database built within R - which tracks the weight of 50 chicks on four different diets over time.

If we want to see the growth in weight of each chick depending on their diet, we can use the following ggplot code:

This chart is still too ‘noisy’, and could benefit on having an animation built in to show progress over the 21-day experiment period.

Add in the gganimate lines

In the code below, we introduce new functions from gganimate to bring our static chart to life:

Notice that when you run this code, it takes a while to render the visual. This is because RStudio is actually making a gif of your animated chart!

With the animation, it’s more engaging to see how the experiment tracked across the days and which chick grew in weight more than the rest.

Saving our animated chart

To save your new animation, you can call on the anim_save function. The code below saves the last animation you did using a file name of your choice:

Example 2: Australia vs New Zealand - life expectancy and GDP per capita

For this example, we’ll use data from gapminder.org showing the GDP per capita and Life Expectancy of different countries from 1952 to 2007.

The database exists in the gapminder package, so make sure you install that and call it forward via the library command:

library(gapminder)

Plot the static visual

Same as the first example, it’s best practice to plan out our static visual first. If we want to look at Oceania countries (Australia vs New Zealand) specifically, we can have a ggplot code as follows:

The static chart above is already quite compelling, but the story is incomplete since we don’t have a view of the time period data.

Animate the scatterplot

Now we introduce the gganimate lines right after ggplot. For this example, we introduce a new function from gganimate:

With our animated chart, it becomes evident that it was in the 70’s when Australia was able to overtake New Zealand in terms of Life Expectancy and GDP per capita!

Save your animation

Remember to save your animation as a file:

More Learning

This is just the tip of the iceberg when it comes to advanced visualization tools like gganimate. If you’re looking for more, here are a few links that dive deeper into this field:

  1. how to save animation as a video file: https://github.com/thomasp85/gganimate
  2. full list of functions from gganimate: https://gganimate.com/reference/index.html
  3. a great article summarizing other animation packages: https://towardsdatascience.com/animating-your-data-visualizations-like-a-boss-using-r-f94ae20843e3