How to make beautiful animated plots using gganimate and tweenr

This code is for creating plots that build up over time (i.e., the x-axis)

library(ggplot2)
library(gganimate)
library(animation)
library(tweenr)

First, load your data.

You data should include at least 4 coloumns:

  1. A variable of interest (e.g., accuracy scores), which will be the y-axis

  2. A variable representing time (e.g., trial number), which will be the x-axis

  3. A varaible representing individual data points (e.g., participant number)

  4. A variable representing condition to which individuals are assigned (e.g., male vs. female)

data <- read.csv("animated_plot_data.csv") 

head(data,30) # show the first 30 rows of the data frame
##    Trial.Number Participant Condition Accuracy
## 1             1           1    Female       11
## 2             1           2    Female       13
## 3             1           3    Female        6
## 4             1           4    Female       17
## 5             1           5    Female        2
## 6             1           6    Female        7
## 7             1           7      Male       11
## 8             1           8      Male       34
## 9             1           9      Male       11
## 10            1          10      Male       27
## 11            1          11      Male        6
## 12            1          12      Male       14
## 13            2           1    Female       46
## 14            2           2    Female       16
## 15            2           3    Female       13
## 16            2           4    Female       25
## 17            2           5    Female        8
## 18            2           6    Female       26
## 19            2           7      Male       26
## 20            2           8      Male        4
## 21            2           9      Male       13
## 22            2          10      Male       32
## 23            2          11      Male       33
## 24            2          12      Male       14
## 25            3           1    Female       59
## 26            3           2    Female       32
## 27            3           3    Female       28
## 28            3           4    Female       41
## 29            3           5    Female       16
## 30            3           6    Female       25

Then, use the tweenr package to make a smooth transition between each time point.

The “tween_elements functions will create two additional coloums to your data frame:”.frame" (corresponding to the origina trial number) and “.group” (corresponding to the original participant number).

data$ease <- "linear" #add a coloumn to the data frame

data2<- tween_elements(data, 'Trial.Number', 'Participant', 'ease', nframes = 100) # create 100 frames between each trial number

head(data2,30) # show the first 30 rows of the data frame
##      Trial.Number Condition  Accuracy .frame .group
## 1        1.000000    Female 11.000000      0      1
## 910      1.000000      Male 27.000000      0     10
## 1011     1.000000      Male  6.000000      0     11
## 1112     1.000000      Male 14.000000      0     12
## 102      1.000000    Female 13.000000      0      2
## 203      1.000000    Female  6.000000      0      3
## 304      1.000000    Female 17.000000      0      4
## 405      1.000000    Female  2.000000      0      5
## 506      1.000000    Female  7.000000      0      6
## 607      1.000000      Male 11.000000      0      7
## 708      1.000000      Male 34.000000      0      8
## 809      1.000000      Male 11.000000      0      9
## 2        1.166667    Female 16.833334      1      1
## 911      1.166667      Male 27.833333      1     10
## 1012     1.166667      Male 10.500000      1     11
## 1113     1.166667      Male 14.000000      1     12
## 103      1.166667    Female 13.500000      1      2
## 204      1.166667    Female  7.166667      1      3
## 305      1.166667    Female 18.333333      1      4
## 406      1.166667    Female  3.000000      1      5
## 507      1.166667    Female 10.166667      1      6
## 608      1.166667      Male 13.500000      1      7
## 709      1.166667      Male 29.000000      1      8
## 810      1.166667      Male 11.333333      1      9
## 3        1.333333    Female 22.666667      2      1
## 912      1.333333      Male 28.666667      2     10
## 1013     1.333333      Male 15.000000      2     11
## 1114     1.333333      Male 14.000000      2     12
## 104      1.333333    Female 14.000000      2      2
## 205      1.333333    Female  8.333333      2      3

Now, we’ll create our wanted plot using ggplot.

plot<- ggplot(data=data2, aes(x = Trial.Number, y = Accuracy, group=.group, color=Condition, frame=.frame, cumulative=TRUE))+
  geom_line(lwd=1, alpha=0.8)+  #change line trasparency with alpha = x
  scale_x_continuous("Trial Number", breaks=c(1:17))+
  scale_y_continuous("Accuracy")+
  theme_classic()+
  theme(text = element_text(size = 25), axis.text.x = element_text(size=20), legend.position=c(.8,.2))

And finally, let’s animate the plot and save it!

# change the speed of the animation by chanding interval = x

gganimate(plot, interval = 0.05)
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?
# remove the title by frame by adding title_frame = FALSE
gganimate(plot, title_frame = FALSE,filename="animated_plot.gif",ani.height=500, ani.width=700, interval = 0.05) # save as gif
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?
## Output at: animated_plot.gif