Search code examples
ranimationggplot2gifgganimate

How to keep previous layers of data while doing animation in R gganimate?


I'm doing animation using ggplot and gganimate. In the previous version of gganimate there was an option "cumulative", seem that new version does not support this.

Here's the code:

library(ggplot2)
library(gganimate)

x = data.frame(y = c(2000, 2001), x=c(1,2), z=c(3,4))
ggplot(x, aes(x,z))+geom_point() + transition_time(y)

It works, but I want to keep the first data point at the scatterplot.

I tried to trasform the data, but it doesn't help:

x1 = data.frame(y = c(2000, 2001, 2001), x=c(1,2,1), z=c(3,4,3))
ggplot(x1, aes(x,z))+geom_point() + transition_time(y)

Solution

  • Does shadow_mark() achieve your desired behavior?

    x = data.frame(y = c(2000, 2001, 2002), x=c(1,2,3), z=c(3,4,5))
    
    p <- ggplot(x, aes(x, z)) +
      geom_point() +
      transition_time(y) +
      shadow_mark()
    
    animate(p)
    

    enter image description here

    It doesn't capture the "tween-ing" but it does leave a point at location combinations in data.