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?
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.
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.
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.
@Stibu's answer is really good. Here are some extra tips:
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()
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
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"/>
https://cran.r-project.org/web/packages/gganimate/gganimate.pdf#page=4
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With