Skip to main content
v2026.11,986 entries · CC-BY 4.0

Animation in R: gganimate and animation Package Guide

How to make animation in R with gganimate or the animation package: install, render, save a GIF or video, and choose between the two.

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

  1. Load the packages. Run library(ggplot2) and library(gganimate).
  2. Build a static ggplot. Create the plot as usual. The example from the gganimate documentation uses the built-in mtcars data: ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot().
  3. Add a transition. Choose the function that matches your data: transition_states(gear, transition_length = 2, state_length = 1) animates between the discrete values of gear. Use transition_time() instead when the variable is continuous, such as years.
  4. Optionally add enter, exit and easing. The documentation example adds enter_fade(), exit_shrink() and ease_aes('sine-in-out').
  5. 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.

  6. Render with control, if needed. animate() renders the frames and passes them to a renderer. The default is gifski_renderer(), which produces a GIF; ffmpeg_renderer() produces video.
  7. Save the result. anim_save() writes the last rendered animation to a file, in the same way ggsave() 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() (the gifski package is listed under Suggests on CRAN), and video rendering with ffmpeg_renderer() requires FFmpeg. Install the one that matches your target format.
  • saveGIF() or saveVideo() fails. In the animation package 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 and transition_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)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Animation in R: gganimate and animation Package Guide

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →