Simple Way to Create Animated Charts in R
A simple way to create animated charts in R is to use the gganimate extension, available as part of ggplot2. It permits you to display your data dynamically. If you are familiar with ggplot2, all that is necessary is to add a transition function at the end and set the parameters for the animation. More details on the full potential of animation elements as a part of gganimate can be found here¹.
In the example below, I will use blog stats to create a simple animated line chart that shows the number of views/reads of published articles for a given time period.
The dataset has the following variables:
1) Date
2) Counts (number of visits)
3) Type (views vs. reads)
To create the line chart, it is necessary to load the following packages — ggplot2, lubridate, ggrepel and gganimate. To customize the look of your animated plot, you can also consider loading ggthemes.
Before making the chart, it is necessary to set the “date” variable to the right format in R.
Step 1:
This is how it looks.
Step 2:
This is the output:
The next step is to produce the read ratio chart. First, it is necessary to reshape the dataset and create new variables: ratio, rank and week.
Step 1:
Step 2:
Step 3:
This is the output:
[1]: Thomas Lin Pedersen. (July 8 , 2020). A Grammar of Animated Graphics https://cran.r-project.org/web/packages/gganimate/gganimate.pdf