Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save frames of gif created using gganimate package

Tags:

r

gganimate

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?

like image 579
Andrew Bade Avatar asked Mar 07 '18 15:03

Andrew Bade


People also ask

What is another way you can save your gganimate plot other than as a GIF input the file extension?

In order to save the animation to a specific location, you can use the anim_save() function which, like ggplot2::ggsave , defaults to taking the last rendered animation and writes it to a file.

How do I save an animated plot in R?

If you need to save the animation for later use you can use the anim_save() function. It works much like ggsave() from ggplot2 and automatically grabs the last rendered animation if you do not specify one directly.

How many frames can a GIF have?

The number of frames per second in your GIF will determine the total number of frames rendered in the final image. Therefore, if your frame rate is 30, the size will be larger than that with a frame rate of 24. Standard GIFs run between 15 and 24 frames per second.


2 Answers

@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

like image 26
stevec Avatar answered Oct 27 '22 00:10

stevec


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.)

like image 52
Stibu Avatar answered Oct 26 '22 22:10

Stibu