R Tip of the Day Example

The gganimate package

Paul Roback

2025-10-16

An intro to gganimate

An intro to gganimate

Code with no animation

gapminder |>
  ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, color = 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(
    x = "GDP per capita",
    y = "life expectancy"
  )

Code with animation

# may need to make sure gifski and av packages are installed!
gapminder |>
  ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, color = 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) +
  transition_time(year) +
  ease_aes('linear') +
  labs(
    title = "Year: {frame_time}",
    x = "GDP per capita",
    y = "life expectancy"
  )

Line Highlighting

  • Incrementally see what we just added
# may need to make sure gifski and av packages are installed!
gapminder |>
  ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, color = 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) +
  transition_time(year) +
  ease_aes('linear') +
  labs(
    title = "Year: {frame_time}",
    x = "GDP per capita",
    y = "life expectancy"
  )

Executable Code

  • Now let us run the code and show the output in our presentation

Code with no animation: Example 2

gapminder07 <- filter(gapminder, year == 2007)
gapminder07 |>
  ggplot(aes(x = lifeExp, y = continent, fill = continent)) +
  geom_density_ridges(show.legend = FALSE, alpha = 0.5) +
  labs(
    title = "Life Expectancy by Continent",
    subtitle = "Year: 2007",
    x = "Life Expectancy",
    y = "Continent"
  )

Code with animation: Example 2

gapminder |>
  ggplot(aes(x = lifeExp, y = continent, fill = continent)) +
  geom_density_ridges(show.legend = FALSE, alpha = 0.5) +
  labs(
    title = "Life Expectancy by Continent",
    subtitle = "Year: {frame_time}",
    x = "Life Expectancy",
    y = "Continent"
  ) +
  transition_time(year) +
  ease_aes("linear")

Line Highlighting

  • Incrementally see what we just added
gapminder |>
  ggplot(aes(x = lifeExp, y = continent, fill = continent)) +
  geom_density_ridges(show.legend = FALSE, alpha = 0.5) +
  labs(
    title = "Life Expectancy by Continent",
    subtitle = "Year: {frame_time}",
    x = "Life Expectancy",
    y = "Continent"
  ) +
  transition_time(year) +
  ease_aes("linear")

Executable Code

  • Now let us run the code and show the output in our presentation

Here’s more executable code: Example 3

Here’s more executable code: Example 4

Here’s more executable code: Example 5

Incremental Lists

Lists can optionally be displayed incrementally:

  • Transitions: between years, categories, etc.
    • transition_time(), transition_states(), transition_reveal()
  • Views: axes can shift with changing data
    • view_follow()
  • Shadows: have a trace or memory of previous data
    • shadow_wake(), shadow_mark()