Creating animated charts in R using gganimate

Sanjeev Kumar
YML Innovation Lab
Published in
3 min readNov 3, 2020

This is follow up blog to Gold returns over different periods. In this blog I would like to show how to create animated charts with facets in R.

Install the following libraries and load them into R:

  1. ggplot2 for creating the plots,
  2. ggthemes for customized themes
  3. zoo for creating rolling means
  4. gganimate to create animations in the chart.

Download the excel file for Gold prices and read the dataset into R as a dataframe. The dataframe should contain the Year column through which we want to analyse and the annual change in price(y column). type column is to mention one year rolling mean.

1 Year rolling mean

Now we need to calculate the rolling means for the windows 5, 10 and 20 years. Create separate dataframes and use the roll mean function in zoo library to create the rolling means and assign the type column to be 5 Years, 10 Years, 20 Years respectively.

Bind the dataframes rowwise using rbind the columns to get the final dataframe which should contain rows like the following:

5 Year rolling means
10 Year rolling means
20 Year rolling means

Create a change column with labels negative and positive based on the y column.

Once the dataset is ready we can plot. First create a geom_bar plot using ggplot function. Use facet_grid function to create the facets of type vertically stacked. To color the bars in the plot provide the change column as fill and assign the colors to the plot using scale_fill_manual function. I have provided red and green color to negative and positive change respectively.

Now comes the animation part. Use the transition_states method which can be added the ggplot chart to animate the plot. Provide the Year column to the transition_states function to animate the change in each Year. This transistion splits the data into multiple states based on the provided column(Year in our case) and animates between states. shadow_mark function makes it leave a shadow along the trail which will have previously animated states. Without the shadow_mark you would just see the chart animating one frame at a time.

You can modify the easing of the animation using ease_aes function.

I have applied Highcharts JS Theme using theme_hc function available from ggthemes library and also set custom font sizes and spacings in the chart.

Finally to export the chart I have used anim_save function which is a analogous function to ggsave. anim_save saves the chart as gif. By default the method anim_save save the plot with very low resolution which cant be used for showcasing, so I have added the width and height in pixels to get the best resolution.

Here is the final animated chart

Animated chart created using gganimate

One can also animate through the facets by providing the column type to the trainsition_states. But there to provide both the Year column and type column, since trainsition_states doesn’t expect a vector.

--

--