---
title: Animations
---

```{r}
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)`.

## Fade-in Animation

```{r}
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))
```

## Wave-in Animation

```{r}
p |> animate(enter = list(type = 'waveIn', duration = 1600))
```

## Grow-in-Y Animation

```{r}
p |> animate(enter = list(type = 'growInY', duration = 2000))
```

## Scale-in-X Animation

```{r}
p |> animate(enter = list(type = 'scaleInX', duration = 1600))
```

## Scale-in-Y Animation

```{r}
p |> animate(enter = list(type = 'scaleInY', duration = 1600))
```

## Zoom-in Animation

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

## Custom Duration and Delay

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

## Disable Animation

```{r}
pm |> animate(FALSE)
```

## Animation on a line chart

```{r}
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))
```
