Animated time series plots in R

A quick guide to animating your time series

Valentine Chisango
Towards Data Science

--

A few weeks back I published an article analyzing the mobility trends in South Africa during 2020. Included in that article was a collection of animated time series plots that traced out the mobility trends over time to paint the picture of how mobility looked in the year marked by covid-19 lockdowns. In this article I will provide a walkthrough of the code for my analysis, focusing on creating animated time series plots like the one below.

Mobility trends for places of residence and work | Graph by author

The data we will use can be found on Google’s covid-19 community mobility reports page and has been included in my GitHub repository as well. Once the .csv file has been loaded into R, we need to split out the national level data. In addition to the plots, the analysis included a comparison table for the average change in mobility for each period of lockdown. This was achieved by first removing the data points that correspond to South African public holidays, which are likely outliers. Next, the remaining data was split into each alert level, and finally, the averages were computed for each alert level.

Calculating average change in mobility in each alert level | Columns 9 to 14 of the dataset represent each of the mobility categories

There are a few packages that are needed to plot the graphs:

  • ggplot2: used to make our plots
  • ggeasy: used to provide easier access to some ggplot2 commands
  • ggpubr: used to format our ggplot2 plots and make them publication ready
  • gganimate: used to animate our ggplot2 plots
  • gifski: used to convert our animated ggplot2 plots into the GIF format
  • transformr: used to smoothly animate the time series paths in the plots
  • scales: used to allow for percentage labels on the y-axis of the plots

The other element of setup for our plots is setting up the markers for each alert level, which we will plot as vertical lines with text labels:

The code for the plot should look familiar to those who have used ggplot2, apart from the very last time. We choose our national dataset, map our aesthetic to have the date on the x-axis and the percentage change in mobility on the y-axis, add another time series on the same axis, add axis labels, set the colours for our lines and include our vertical lines to segment the alert levels. The last line (line 15 in the snippet below) is all that we need to animate the plot. The parameter we supply to the transition_reveal function is the same as the x-axis aesthetic in our graphs because we want to create a gradually revealing time series.

Code to generate the Mobility trends for places of residence and work plot seen above

Finally, we render the plot to a GIF using the animate function and use anim_save (which by default saves the last rendered graphic) to save our animated time series plot as a GIF. The file will be saved into the same directory as the R file we are working with.

Rendering an animated plot as a GIF and saving the .gif file

The remainder of the plots are generated in the same fashion and the complete code is available in the repository here.

--

--