Andrew Bade
Andrew Bade

Reputation: 323

How to save frames of gif created using gganimate package

I will use the gapminder data as an example. Let's say I create this animation:

library(gapminder)
library(ggplot2)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = 
continent, frame = year)) +
  geom_point() +
  scale_x_log10()

library(gganimate)

gganimate(p)

gganimate(p, "output.gif")

Now, I want to have access to the individual images (frames) that constitute the gif. Is there a way to do this in gganimate or do I need to use the animation package?

Upvotes: 6

Views: 7471

Answers (2)

stevec
stevec

Reputation: 52268

@Stibu's answer is really good. Here are some extra tips:

For smoother animations

  • set nframes to a multiple of the number of individual plots in the animation. For example, if you had 52 plots in the animation (one for each week of the year), try setting nframes = (4 * 52), or nframes = (6 * 52) etc.

  • try adding enter_grow() and exit_fade() if you haven't already (they can be added to many animations without parameters)

myanimation + 
  enter_grow() +
  exit_fade()

Speed up / Slow down

  • If you selected a high nframe, your animation may be slow. You can change the speed of your animation by setting an appropriate duration, e.g.
animate(myanimation, 
  nframes = 312, 
  renderer = gifski_renderer("new_users_weekly.gif"), 
  duration = 14) # Duration in seconds

Using the saved gif in an RMarkdown (or other webpage)

Inserting the .gif that results from

animate(myanimation, renderer = gifski_renderer("new_users_weekly.gif")

into a webpage or RMarkdown can be done by simply:

<img src="new_users_weekly.gif" alt="animation"/>

More info

https://cran.r-project.org/web/packages/gganimate/gganimate.pdf#page=4

Upvotes: 3

Stibu
Stibu

Reputation: 15907

gganimate has changed a lot since this question was asked. In the current version (0.9.9.9999), there is a way to store each frame as its own file.

First, I need to create the animation, which looks a bit different with the new version of the package:

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
      geom_point() +
      scale_x_log10() +
      transition_states(year, 1, 5)

The animation can then be shown using

animate(p)

The rendering is taken care of by so called renderers. To store the animation in a single animated gif, you can use

animate(p, nframes = 24, renderer = gifski_renderer("gganim.gif"))

Note that I have manually set the number of frames to be created. By default, 100 frames are used and I chose a smaller number here. Picking the right number of frames can be a bit tricky at times and if you get weird results, try using more frames.

Alternatively, you can use a file_renderer() to write each frame to its own file

animate(p, nframes = 24, device = "png",
        renderer = file_renderer("~/gganim", prefix = "gganim_plot", overwrite = TRUE))

This will write files named gganim_plot0001.png, gganim_plot0002.png, etc. to the directory ~/gganim. Modify the values for prefix and device if you want different file names or different file types. (I set them to the defaults.)

Upvotes: 16

Related Questions