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:
ChickWeights dataset - an experiment on different poultry diets, tracking the weight of chicks over 21 days
Gapminder dataset - Life expectancy and GDP per capita of Australia and New Zealand from 1952 to 2007
First things first - ensure that you have ggplot2 (included in tidyverse) and gganimate packages installed, then call them via the library command:
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:
ChickWeight %>%
ggplot(aes(weight, Chick, color=Diet)) +
#plot weight on x-axis
#plot chick reference number on y-axis
#then differentiate the colors based on which Diet they are on
geom_point() #use scatterplot to show weights
This chart is still too ‘noisy’, and could benefit on having an animation built in to show progress over the 21-day experiment period.
In the code below, we introduce new functions from gganimate to bring our static chart to life:
ChickWeight %>%
ggplot(aes(weight, Chick, color=Diet)) +
geom_point() +
#here comes the gganimate code
labs(title = 'Days: {frame_time}', x = 'Chick weight (grams)', y = 'Chick number') +
transition_time(Time) +
ease_aes('linear')
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.
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:
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:
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:
gapminder %>%
filter(continent=="Oceania") %>% #filter to just AU & NZ
ggplot(aes(gdpPercap, lifeExp, size=pop, colour = country)) + #plot x & y axes, size, and color
geom_point(alpha = 0.5, show.legend = TRUE) + #use scatter plot
scale_size(range = c(2,12)) #make sizing more prominent
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.
Now we introduce the gganimate lines right after ggplot. For this example, we introduce a new function from gganimate:
gapminder %>%
filter(continent=="Oceania") %>%
ggplot(aes(gdpPercap, lifeExp, size=pop, colour = country)) +
geom_point(alpha = 0.5, show.legend = TRUE) +
scale_size(range = c(2,12)) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita in USD', y = 'Life Expectancy') +
transition_time(year) +
shadow_wake(.3)+
ease_aes('linear')
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!
Remember to save your animation as a file:
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: