Animate 'ggplot2' Plots with 'Anime.js'

Renders 'ggplot2' plots written with 'gganimate' syntax as animated Scalable Vector Graphics (SVG) drawn in the browser by the 'Anime.js' JavaScript library (< https://animejs.com>), through the 'animejs' package. The result is a single self-contained, resolution-independent 'htmlwidget' with one timeline over the plot's elements.


gganime

CRANstatus R-CMD-check Codecov testcoverage

gganime renders a ggplot2 plot written with gganimate syntax as an animated SVG. The output is a self-contained HTML widget powered by Anime.js (through the animejs package), instead of a GIF or a video.

Because the animation is a single SVG with a timeline over its attributes:

  • it stays sharp at any size, since the marks are vectors rather than a raster of fixed pixels;
  • the file holds one copy of the scene plus per-element keyframes, not one image per frame;
  • it plays in the browser with a scrub bar, so a reader can pause and step through frames.

Installation

install.packages("gganime")

Or the development version from GitHub:

# install.packages("pak")
pak::pak("long39ng/gganime")

Usage

Write the plot exactly as you would with gganimate, then call anime() instead of animate(). Here transition_time() animates over a continuous variable, sizing each country’s bubble by population and filling in the {frame_time} label between years:

library(ggplot2)
library(gapminder)
library(gganime)

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  labs(title = "Year: {frame_time}", x = "GDP per capita", y = "life expectancy") +
  transition_time(year) +
  ease_aes("linear")

anime(p)
Bubble chart of life expectancy against GDP per capita, one bubble per country sized by population, faceted by continent, animating across years.

A shadow_mark() leaves earlier frames behind the current one:

p <- ggplot(airquality, aes(Day, Temp)) +
  geom_point() +
  transition_time(Month) +
  shadow_mark(colour = "grey70") +
  labs(title = "Month: {frame_time}")

anime(p)
Scatterplot of daily temperature over a month, with earlier days left behind in grey as the animation advances.

anime() returns an htmlwidget, so it prints in the RStudio Viewer, embeds in R Markdown and Quarto, and saves with htmlwidgets::saveWidget(). Loading gganime also makes a bare gganimate plot print through anime() at the console; set options(gganime.autoprint = FALSE) to keep gganimate’s own output.

In Shiny, use gganimeOutput() and renderGganime().

Supported features

  • Transitions: transition_states(), transition_time(), transition_reveal().
  • Geoms: points, lines and paths, bars and columns, areas and ribbons.
  • Point shapes: any pch R draws from a single shape – circles, squares, triangles, diamonds – animates its size and outline width. The shapes drawn from several parts (pch 3, 4, and 7 to 14) hold their size.
  • Several layers in one plot, including two of the same kind, and a static layer beside an animated one.
  • Facets: facet_wrap() and facet_grid(), with fixed or free scales.
  • Coords: coord_cartesian(), coord_fixed() / coord_equal(), and coord_flip().
  • enter_*() / exit_*(), ease_aes(), and per-frame labels in the title, subtitle, and caption.
  • shadow_mark() and shadow_trail().

Anything else stops with a message naming the alternative. Current limits include non-Cartesian coordinate systems, shadow_wake(), and view_*() other than view_static().

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("gganime")

0.1.0 by Long Nguyen, 2 months ago


https://github.com/long39ng/gganime, https://long39ng.github.io/gganime/


Report a bug at https://github.com/long39ng/gganime/issues


Browse source code at https://github.com/cran/gganime


Authors: Long Nguyen [aut, cre]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports animejs, cli, gganimate, ggplot2, grDevices, grid, gridSVG, rlang, utils, xml2

Suggests jsonlite, knitr, rmarkdown, shiny, testthat, withr


See at CRAN