Animations

library(gglite)
options(gglite.defer_render = TRUE)

Animations control how marks enter, update, and exit the chart. Use animate() to configure them on the most recently added mark.

Note: the charts on this page use options(gglite.defer_render = TRUE), which defers rendering until the chart is scrolled into view so that the animation plays when you first see it. The durations are also set longer than you would typically use, so the animations are easier to see. In your own plots you may want to use shorter durations and omit options(gglite.defer_render = TRUE).

1 Fade-in Animation

df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2))
p = g2(df, y ~ x)
p |> animate(enter = list(type = 'fadeIn', duration = 2000))

2 Wave-in Animation

p |> animate(enter = list(type = 'waveIn', duration = 1600))

3 Grow-in-Y Animation

p |> animate(enter = list(type = 'growInY', duration = 2000))

4 Scale-in-X Animation

p |> animate(enter = list(type = 'scaleInX', duration = 1600))

5 Scale-in-Y Animation

p |> animate(enter = list(type = 'scaleInY', duration = 1600))

6 Zoom-in Animation

pm = g2(mtcars, hp ~ mpg)
pm |> animate(enter = list(type = 'zoomIn', duration = 1200))

7 Custom Duration and Delay

p |> animate(enter = list(type = 'fadeIn', duration = 3000, delay = 1000))

8 Disable Animation

pm |> animate(FALSE)

9 Animation on a line chart

df = data.frame(x = 1:10, y = c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3))
g2(df, y ~ x) |>
  mark_line() |>
  animate(enter = list(type = 'fadeIn', duration = 2000))