gganimate: A Lesson with Avocados

Kelsey Gonzalez

May 25, 2020

A repo of this material is available here.

Animating your ggplots may sound daunting. However, you have to add a line or two extra of code and you have an animation! gganimate makes animation quite accessible for users of ggplot.

A cheat sheet for what we’ll cover today:

  • Types of Transitions
    • transition_reveal() (good for mainly geom_line)
    • transition_states() (good for geom_point, geom_bar, geom_col, etc)
      • splits up plot data by a discrete variable and animates between the different states.
    • transition_time() (geom_point)
      • Instead of transitioning over a discrete variable, the transition occurs over a continuous variable (Time)
  • Some extras
    • shadow_mark()
      • This shadow lets you show the raw data behind the current frame. Both past and/or future raw data can be shown and styled as you want.
    • shadow_wake()
      • instead of leaving a more permanent mark, wake leaves a tail to show direction, but only reveals the past few states
    • view_follow(fixed_y = TRUE)
      • controls how your viewport changes as the transition occurs
    • anim_save()
      • It works much like ggsave() from ggplot2 and automatically grabs the last rendered animation if you do not specify one directly.

Let’s load back up our data from the previous lessons on R by Adriana Picoral (picoral.github.io/resbaz_intro_to_r/parti.html) and from Kathryn Busby on ggplot2. I’ll name the dataframe avocado because I can’t remember what the other instructors named their data. We will also load our packages here.

library(tidyverse)
# install.packages("gganimate")
library(gganimate)
# install.packages("scales")
library(scales)
avocado <- read_csv("avocado.csv")