Overview

Creating graphics that can be animated are great tools for storytelling. This code-through breaks down the steps necessary to make your own animated plots in R using the ggplot2 and gganimate packages.

Installation and Setup

With a clean installation of R and RStudio, ggplot2 and gganimate must be installed to follow along. Tidyverse contains ggplot2 and dplyr which are both used below. Additionally, gganimate and gifski are required to animate and save motion graphics in .gif format. The gapminder and Lahman packages contain panel data used to plot the graphs in this code-through.

To load the newly install packages, execute the library function for each of the following packages:

Gapminder

Loading the data from the Gapminder Foundation and verifying the structure of each set will help to identify the variables needed for ggplot().

## Classes 'tbl_df', 'tbl' and 'data.frame':    1704 obs. of  6 variables:
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num  28.8 30.3 32 34 36.1 ...
##  $ pop      : int  8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num  779 821 853 836 740 ...

Plotting and Animating

Using the gapminder variables lifeExp and gdpPercap, a simple plot can be created using the ggplot and geom_point functions. The color of points match depending on the continent, while the size of each point is different according to country population. Additionally, the legend can be removed by adding (+) the theme function.

Animating must be added (+) to the ggplot function. The plot as well as x and y axes can be labeled. Transition_time must be a numeric unit of time. Coord_flip swaps the x and y axes and ease_aes controls how the plotted points move as a function of time. Linear is the default and shows a smooth transition while something like elastic moves the points in quick, jerky motion.

Lahman

Loading the data from the Lahman Baseball Database and verifying the unique variables of each set will help to identify the variables needed for ggplot().

## 'data.frame':    26428 obs. of  5 variables:
##  $ yearID  : int  1985 1985 1985 1985 1985 1985 1985 1985 1985 1985 ...
##  $ teamID  : Factor w/ 35 levels "ANA","ARI","ATL",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ lgID    : Factor w/ 2 levels "AL","NL": 2 2 2 2 2 2 2 2 2 2 ...
##  $ playerID: chr  "barkele01" "bedrost01" "benedbr01" "campri01" ...
##  $ salary  : int  870000 550000 545000 633333 625000 800000 150000 483333 772000 250000 ...

Plotting and Animating

Similar to the plot above, the variables used are plotted. However, there are key changes with the style of the graph and animation. The use of geom_path and geom_point gives the ability to watch the point move in time with the trailing line. While not noticeable in this visualization, size does affect the line and point size.

Animating this graph over time requires a different a different transition. The transition_reveal function requires a statement of movement. In this case, the reveal is along the year axis. The ease_aes could be set different to speed up or slow down the reveal, but has been left as linear.

Saving Animations

Additionally, animations can be saved with the function anim_save as seen below and will be placed in the working directory unless given a path within the function. Thanks for making it to the end and good luck with creating animations in R!

Resources

To change the options in ggplot() or gganimate(), the resources used to build this code-through are included below:

https://ggplot2.tidyverse.org/
https://cran.r-project.org/web/packages/gganimate/gganimate.pdf