Search code examples
rgganimate

latest gganimate: How to have a fixed plot in the background?


In the latest version of gganimate by https://github.com/thomasp85 I would like to choose which parts of the plot I can keep static throughout the animation and which will be animated. In the previous version of gganimate you could specify the frame in the aes of ggplot. Thus you could create a base plot that would be static and plot the animated plot over this. How can similar be achieved in the latest version?


Solution

  • This has already been addressed in an issue for gganimate on GitHub: https://github.com/thomasp85/gganimate/issues/94

    Basically you specify the the layers that are meant to be static with a separate data frame from the one you initially passed to ggplot. The example in the GitHub ticket I referred to is

    library(gganimate)
    #> Loading required package: ggplot2
    ggplot(dat = data.frame(x=1:10,y=1:10), aes(x=x,y=y)) +
      geom_point() +
      geom_line(data = data.frame(x2 = 1:10, y = 1:10),
                aes(x = x2, y = y, group = 1)) +
      transition_time(x)
    animate(last_plot(), nframes = 50)
    

    Here the line is held static, while the point is moving.