Written and maintained by CASRAI Editorial Board
Published
On this page
Animation in R means producing a sequence of plot frames and saving them as a GIF, video or other animated file. In R there are two established routes: the gganimate package, which animates ggplot2 plots by adding transition functions to the plot, and the animation package, which saves frames you generate yourself in a loop. This guide is for researchers who already have a static R plot and need to show change over time or across states, and shows how to install each package, render a first animation, save it, and choose between them.
What animation in R is used for in research
Animated plots are used to show how a variable changes across time (for example, yearly values) or across discrete groups (for example, experimental conditions) without laying out many separate panels. In gganimate, the transition function you add to a plot defines how the data are spread over time: transition_states() splits the data into discrete states, transition_time() maps a continuous variable such as years to time, and transition_manual() lets you specify frames directly. Animated figures suit presentations, supplementary files and web pages; a journal article’s main figures usually need a static version as well. For static chart design principles, see great data visualization examples, and for a classic dataset often animated over years, see Gapminder.
Install or access
gganimate
Install the released version from CRAN:
install.packages('gganimate')
The development version is installed from GitHub with the pak package:
install.packages('pak')
pak::pak('thomasp85/gganimate')
On CRAN, gganimate 1.0.11 (published 2025-09-04, MIT + file LICENSE) depends on ggplot2 (≥ 3.5.2) and imports transformr and tweenr. Its suggested packages include av, gifski (≥ 1.4.3), magick, ragg, sf, svglite, knitr and rmarkdown.
animation package
install.packages('animation')
CRAN lists animation 2.8 (published 2025-08-26), described as providing functions to save animations as GIF, HTML, PDF, video and Flash. It depends on R ≥ 2.14.0.
Step-by-step: animate a ggplot2 plot with gganimate
- Load the packages. Run
library(ggplot2)andlibrary(gganimate). - Build a static ggplot. Create the plot as usual. The example from the gganimate documentation uses the built-in
mtcarsdata:ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(). - Add a transition. Choose the function that matches your data:
transition_states(gear, transition_length = 2, state_length = 1)animates between the discrete values ofgear. Usetransition_time()instead when the variable is continuous, such as years. - Optionally add enter, exit and easing. The documentation example adds
enter_fade(),exit_shrink()andease_aes('sine-in-out'). - Print the object. The complete documentation example is:
library(ggplot2) library(gganimate) ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot() + transition_states(gear, transition_length = 2, state_length = 1) + enter_fade() + exit_shrink() + ease_aes('sine-in-out')Printing the plot object renders the animation.
- Render with control, if needed.
animate()renders the frames and passes them to a renderer. The default isgifski_renderer(), which produces a GIF;ffmpeg_renderer()produces video. - Save the result.
anim_save()writes the last rendered animation to a file, in the same wayggsave()saves the last static plot.last_animation()returns the last animation as a GIF image object.
The animation package route
The animation package works differently: you write the loop that draws each frame and the package captures and assembles them. Its saving functions each call an external tool, so the required software depends on the format you choose:
saveGIF()needs ImageMagick, GraphicsMagick or LyX.saveVideo()needs FFmpeg or avconv.saveLatex()needs (PDF)LaTeX.saveSWF()needs SWF Tools.
Output and how to read it
A gganimate call produces an animated GIF by default, with the plot redrawn frame by frame as the transition progresses. In a transition_states() animation, the plot moves from one state to the next, with transition_length and state_length setting the relative time spent moving between states and pausing on each one; ease_aes() sets how values accelerate between frames. Treat the motion between states as interpolation drawn by the software: only the states themselves correspond to observations in your data.
Troubleshooting
- No GIF or video is produced. Each output route depends on a renderer package or external tool. For gganimate, the default GIF renderer is
gifski_renderer()(thegifskipackage is listed under Suggests on CRAN), and video rendering withffmpeg_renderer()requires FFmpeg. Install the one that matches your target format. saveGIF()orsaveVideo()fails. In theanimationpackage these functions call ImageMagick (or GraphicsMagick/LyX) and FFmpeg (or avconv) respectively; confirm the tool is installed and reachable from R.- Wrong transition function. Use
transition_states()for discrete groups andtransition_time()for a continuous time variable. - Version mismatches. gganimate 1.0.11 requires ggplot2 ≥ 3.5.2; update ggplot2 if installation or loading complains.
Free alternatives: gganimate and the animation package
| Question | gganimate | animation |
|---|---|---|
| Approach | Adds transition functions to a ggplot2 plot | Saves frames that you draw in a loop |
| CRAN version checked (2026-11-08) | 1.0.11 (2025-09-04) | 2.8 (2025-08-26) |
| Output formats | GIF by default (gifski_renderer()); video via ffmpeg_renderer() |
GIF, HTML, PDF, video, Flash |
| External software for output | Renderer packages such as gifski; FFmpeg for video | ImageMagick/GraphicsMagick/LyX (GIF), FFmpeg/avconv (video), (PDF)LaTeX (LaTeX), SWF Tools (SWF) |
| Saving | anim_save() |
saveGIF(), saveVideo(), saveLatex(), saveSWF() |
Both are CRAN packages. If your figures are already ggplot2 plots, gganimate lets you keep that code and add a transition; the animation package fits cases where frames come from non-ggplot2 drawing code.
Reproducibility note
An animated figure is a computed output, so record the R, ggplot2 and gganimate versions (for example with sessionInfo()) and keep the script that generates it alongside the saved file written by anim_save(). See computational reproducibility for the wider concept. If you are preparing data first, see data frames in R.
FAQ
What is gganimate?
gganimate is an R package that extends ggplot2 with transition, enter/exit and easing functions so that a plot can be rendered as an animation. CRAN lists version 1.0.11, published 2025-09-04.
How do I make an animation in R?
Load ggplot2 and gganimate, build a plot, add a transition such as transition_states() or transition_time(), and print the plot; then save it with anim_save().
Which R package should I use for animations?
Use gganimate for ggplot2 plots. Use the animation package when you generate frames with your own loop and need to save to formats such as HTML, PDF or video.
How do I save an animation in R?
With gganimate, anim_save() writes the last rendered animation to a file. With the animation package, use saveGIF(), saveVideo(), saveLatex() or saveSWF().
What does R need to render a GIF or video?
gganimate renders GIFs with gifski_renderer() by default and video with ffmpeg_renderer(). The animation package calls ImageMagick, GraphicsMagick or LyX for GIFs and FFmpeg or avconv for video.
Sources (official documentation)
- gganimate documentation, gganimate.com, read 2026-11-08.
- CRAN package page, gganimate 1.0.11, read 2026-11-08.
- CRAN package page, animation 2.8, read 2026-11-08.







