gganimate and Baseball Stats

Will Zatkowski

What gganimate does

  • gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation
  • It does this by providing a range of new grammar classes that can be added to the plot object in order to customize how it should change with time.
  • essentially creating a gif of data overtime

Main Functions

  • transition_*() defines how the data should be spread out and how it relates to itself across time.

  • view_*() defines how the positional scales should change along the animation.

  • shadow_*() defines how data from other points in time should be presented in the given point in time.

  • enter_*()/exit_*() defines how new data should appear and how old data should disappear during the course of the animation.

  • ease_aes() defines how different aesthetics should be eased during transitions.

Example Code

#| echo: true
#| code-fold: true
#| code-summary: "Show the code"

ggplot(data = all_years) +
      geom_point(aes(x = SO, y = HR, color = Division), alpha = 0.7) +
      scale_x_log10() +
      # the gganimate specific bits
      labs(title = 'Year: {frame_time}') +
      transition_time(year, range = c(2015, 2024)) +
      ease_aes('linear')

Example

Another example

Show the code
b <- ggplot(all_years, aes(Lg, HR)) +
  geom_boxplot() +
  # Here comes the gganimate code
  transition_states(
    Division,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() +
  exit_shrink() +
  ease_aes('sine-in-out')

animate(b, renderer = gifski_renderer())

Thanks!