Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate the process of adding layers to a ggplot2 plot

I am starting to get familiar with gganimate, but I want to extend my gifs further.

For instance, I can throw a frame on one variable in gganimate but what if I want to animate the process of adding entirely new layers/geoms/variables?

Here's a standard gganimate example:

library(tidyverse)
library(gganimate)

p <- ggplot(mtcars, aes(x = hp, y = mpg, frame = cyl)) +
    geom_point()

gg_animate(p)

But what if I want the gif to animate:

# frame 1
ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_point()

# frame 2
ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_point(aes(color = factor(cyl)))

# frame 3
ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_point(aes(color = factor(cyl), size = wt))

# frame 4
ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_point(aes(color = factor(cyl), size = wt)) +
    labs(title = "MTCARS")

How might this be accomplished?

like image 918
emehex Avatar asked Oct 06 '16 15:10

emehex


2 Answers

You can manually add a frame aesthetic to each layer, though it will include the legends for all of the frames immediately (Intentionally, I believe, to keep ratios/margins, etc. correct:

saveAnimate <-
  ggplot(mtcars, aes(x = hp, y = mpg)) +
  # frame 1
  geom_point(aes(frame = 1)) +
  # frame 2
  geom_point(aes(color = factor(cyl)
                 , frame = 2)
             ) +
  # frame 3
  geom_point(aes(color = factor(cyl), size = wt
                 , frame = 3)) +
  # frame 4
  geom_point(aes(color = factor(cyl), size = wt
                 , frame = 4)) +
  # I don't think I can add this one
  labs(title = "MTCARS")

gg_animate(saveAnimate)

enter image description here

If you want to be able to add things yourself, and even see how legends, titles, etc. move things around, you may need to step back to a lower-level package, and construct the images yourself. Here, I am using the animation package which allows you to loop through a series of plots, with no limitations (they need not be related at all, so can certainly show things moving the plot area around. Note that I believe this requires ImageMagick to be installed on your computer.

p <- ggplot(mtcars, aes(x = hp, y = mpg))

toSave <- list(
  p + geom_point()
  , p + geom_point(aes(color = factor(cyl)))
  , p + geom_point(aes(color = factor(cyl), size = wt))
  , p + geom_point(aes(color = factor(cyl), size = wt)) +
    labs(title = "MTCARS")
)

library(animation)

saveGIF(
  {lapply(toSave, print)}
  , "animationTest.gif"
 )

enter image description here

like image 195
Mark Peterson Avatar answered Sep 28 '22 09:09

Mark Peterson


The gganimate commands in the earlier answers are deprecated as of 2021 and won't accomplish OP's task.

Building on Mark's code, you can now simply create a static ggplot object with multiple layered geoms and then add the gganimate::transition_layers function to create an animation that transitions from layer to layer within the static plot. Tweening functions like enter_fade() and enter_grow() control how elements change into and out of frames.

library(tidyverse)
library(gganimate)

anim <- ggplot(mtcars, aes(x = hp, y = mpg)) +
  # Title
  labs(title = "MTCARS") +
  # Frame 1
  geom_point() +
  # Frame 2  
  geom_point(aes(color = factor(cyl))) +
  # Frame 3
  geom_point(aes(color = factor(cyl), size = wt)) +
  # gganimate functions
  transition_layers() + enter_fade() + enter_grow()

# Render animation
animate(anim)

like image 23
martinlu Avatar answered Sep 28 '22 07:09

martinlu