## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)

# Fall back to code-only if the render dependencies are missing.
render_ok <- requireNamespace("gganimate", quietly = TRUE) &&
  requireNamespace("gridSVG", quietly = TRUE) &&
  requireNamespace("animejs", quietly = TRUE)

## ----eval = render_ok---------------------------------------------------------
library(ggplot2)
library(gganime)

p <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point()

## ----eval = render_ok---------------------------------------------------------
anim <- p +
  transition_states(Species, transition_length = 2, state_length = 1)

## ----eval = render_ok---------------------------------------------------------
anime(anim, nframes = 30)

## ----eval = render_ok---------------------------------------------------------
aq <- ggplot(airquality, aes(Day, Temp)) +
  geom_point() +
  transition_time(Month)

anime(aq, nframes = 30)

## ----eval = render_ok---------------------------------------------------------
ec <- ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  transition_reveal(date)

anime(ec, nframes = 40)

## ----eval = render_ok---------------------------------------------------------
aq_labelled <- ggplot(airquality, aes(Day, Temp)) +
  geom_point() +
  transition_time(Month) +
  labs(title = "Month: {frame_time}")

anime(aq_labelled, nframes = 30)

## ----eval = render_ok---------------------------------------------------------
aq_two_line <- ggplot(airquality, aes(Day, Temp)) +
  geom_point() +
  transition_time(Month) +
  labs(title = "Temperature by day\nMonth: {frame_time}")

anime(aq_two_line, nframes = 30)

## ----eval = render_ok---------------------------------------------------------
anime(anim + ease_aes("cubic-in-out"), nframes = 30)

## ----eval = render_ok---------------------------------------------------------
anim_species <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(aes(colour = Species), size = 2) +
  transition_states(Species, transition_length = 2, state_length = 1)

anime(anim_species + enter_fade() + exit_shrink(), nframes = 30)

## ----eval = render_ok---------------------------------------------------------
aq_layers <- ggplot(
  airquality[airquality$Month %in% c(5, 6, 7), ],
  aes(Day, Temp)
) +
  geom_area(fill = "#9ecae1") +
  geom_line(linewidth = 1, colour = "#08519c") +
  geom_point(size = 2, colour = "#a50f15") +
  labs(title = "Month {closest_state}") +
  transition_states(Month, transition_length = 2, state_length = 1)

anime(aq_layers, nframes = 30)

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

anime(aq_shadow, nframes = 30)

## ----eval = render_ok---------------------------------------------------------
aq_trail <- ggplot(airquality, aes(Day, Temp)) +
  geom_point() +
  transition_time(Month) +
  shadow_trail(distance = 0.1, max_frames = 5, colour = "grey70") +
  labs(title = "Month: {frame_time}")

anime(aq_trail, nframes = 30)

## ----eval = render_ok---------------------------------------------------------
anime(anim, nframes = 50, duration = 4, loop = FALSE)

## ----eval = FALSE-------------------------------------------------------------
# htmlwidgets::saveWidget(anime(anim), "iris.html")

