gganimate v0.1.1


Monthly downloads



Create easy animations with ggplot2

Create animations with ggplot2 by treating the "frame" as an aesthetic similar to x, y, or color. Compatible with presenting animations in knitr or saving to a video or animated image file. This version is now deprecated.


gganimate: Create easy animations with ggplot2

gganimate wraps the animation package to create animated ggplot2 plots. It can be installed using devtools:


The core of the approach is to treat "frame" (as in, the time point within an animation) as another aesthetic, just like x, y, size, color, or so on. Thus, a variable in your data can be mapped to frame just as others are mapped to x or y.

For example, suppose we wanted to create an animation similar to the Gapminder world animation, using Jenny Bryan's gapminder package for the data.

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

Notice we added frame = year and saved the plot as p. We then display it as an animation with the gganimate function:




This creates a animation you can view within RStudio. When run within knitr (and combined with knitr's = "animate" chunk option), it instead displays each of the figures in sequence to let knitr create an animation as seen above.

You can also save the animation to a file, such as an GIF, video, or an animated webpage:

gganimate(p, "output.gif")
gganimate(p, "output.mp4")
gganimate(p, "output.swf")
gganimate(p, "output.html")

(Each of these requires ffmpeg, ImageMagick, or other such drivers to be installed on your computer: see the animation package documentation for more).

Notice that the axis limits and legend stay fixed between animation frames, even though the points move. This is one of the advantages of the gganimate package as opposed to creating each ggplot separately and combining them with the animation package: the plot (with its axes and legend) is built only once but rendered for each frame.


You can have some layers of your plot animated and others not, simply by adding the frame aesthetic to those layers and not others. This is useful, for example, if you want to highlight particular points in an animation rather than move them.

p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
  geom_point() +
  geom_point(aes(frame = year), color = "red") +



You could also make a layer cumulative by adding an aesthetic cumulative = TRUE, which is useful for building up an object or path over time.

p3 <- ggplot(gapminder, aes(gdpPercap, lifeExp, frame = year)) +
  geom_path(aes(cumulative = TRUE, group = country)) +
  scale_x_log10() +



Note that while it's intuitive to match frame to a "time" dimension, you could match any variable in your data to the frame aesthetic. We could animate across continents instead:

p4 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, frame = continent)) +
  geom_point() +



Note that if there is a stat summarization (such as a geom_smooth) that you want to animate, you should also add a group aesthetic to that layer. Otherwise, the layer will be calculated once across all frames (and therefore be constant in the animation):

p5 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, frame = year)) +
  geom_point() +
  geom_smooth(aes(group = year), method = "lm", show.legend = FALSE) +
  facet_wrap(~continent, scales = "free") +



Finally, note that you can control your animation with the same options described in ani.options, passed directly to gganimate or to ani.options beforehand. For example, you could make the plot faster or slower with the interval argument:

gganimate(p, interval = .2)


However, note that while this works for displaying a plot in RStudio or saving it, within a knitr document you'll need to add interval = .2 to the chunk options. Other options can be added with ani.options run before the chunk in question.

Functions in gganimate

Name Description
gganimate Show an animation of a ggplot2 object
animation_saver Retrieve a function for saving animations based on a string/function or a filename
gganimate_save Save a gganimate object to a file
gganimate_tempfile Create a temporary file within the temporary directory
plot_ggplot_build Plot a built ggplot object
auto_browse Auto browse to a filename
print.gganimate Print a gganimate object, allowing browsing in RStudio
No Results!

Last month downloads


Type Package
Date 2018-06-28
License GPL-2
LazyData TRUE
VignetteBuilder knitr
RoxygenNote 5.0.1

Include our badge in your README