denis
denis

Reputation: 5673

gganimate changes speed (fps) during animation when using end_pause

I noticed something weird, when using gganimate: when using end_pause in animate, it creates an acceleration during the animation:

library(ggplot2)
library(gganimate)
df <- data.frame(year = 1980:2020)
df$x <- 1
df$y <- 1980:2020


anim <- df %>% ggplot(aes(x,y))+
  geom_point()+
  transition_states(
    year,
    transition_length = 0,
    state_length = 20
  )
N <- length(df$year)
animate(anim, renderer = gifski_renderer(),fps = 2,nframes = 2*N,end_pause = 20)

enter image description here

Here, you can see that the pace of the points double after 2002. This does not happens if end_pause is not used. Why ? How can I correct ?

Upvotes: 0

Views: 258

Answers (1)

Jon Spring
Jon Spring

Reputation: 66870

animate(nframes = 2*N, end_pause = 20) is telling gganimate you want 41*2 = 82 frames in total, with the last 20 being copies of the last one. This means you have 41 states to assign to the first 62 frames, which will not be uniform. It looks like gganimate is in this case assigning two frames to the first ~half of the points, but only one frame to each of the last states. We could fix this by accounting for the pause in our frame count.

N <- length(df$year)
P = 20
animate(anim, renderer = gifski_renderer(),fps = 24,nframes = 2*N+P,end_pause = P, height = 200)

With fix:

enter image description here

Without:

enter image description here

Upvotes: 1

Related Questions