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.
gganimate: Create easy animations with ggplot2
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.
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()
Notice we added
frame = year and saved the plot as
p. We then display it as an animation with the
This creates a animation you can view within RStudio. When run within knitr (and combined with knitr's
fig.show = "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:
gg_animate(p, "output.gif") gg_animate(p, "output.mp4") gg_animate(p, "output.swf") gg_animate(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") + scale_x_log10() gg_animate(p2)
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() + facet_wrap(~continent) gg_animate(p3)
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() + scale_x_log10() gg_animate(p4)
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") + scale_x_log10() gg_animate(p5)
Finally, note that you can control your animation with the same options described in ani.options, passed directly to
gg_animate or to
ani.options beforehand. For example, you could make the plot faster or slower with the
gg_animate(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
|gganimate_tempfile||Create a temporary file within the temporary directory|
|auto_browse||Auto browse to a filename|
|animation_saver||Retrieve a function for saving animations based on a string/function or a filename|
|print.gg_animate||Print a gganimate object, allowing browsing in RStudio|
|plot_ggplot_build||Plot a built ggplot object|
|gg_animate||Show an animation of a ggplot2 object|
|gg_animate_save||Save a gganimate object to a file|
|gganimate||Show an animation of a ggplot2 object|
|gganimate_save||Save a gganimate object to a file|
|print.gganimate||Print a gganimate object, allowing browsing in RStudio|
Last month downloads
Include our badge in your README