Extras

Kieran Healy

Duke University

April 17, 2024

Extras

Load our packages

library(here)       # manage file paths
library(tidyverse)  # your friend and mine
library(socviz)     # data and some useful functions
library(ggrepel)    # Text and labels
library(colorspace) # luminance-balanced palettes
library(scales)      # scale adjustments and enhancements
library(ggforce)    # useful enhancements to ggplot

## install.packages("gifski")
## install.packages("gganimate")

library(gapminder)
library(gganimate) # Animation

Gapminder returns

p <- ggplot(data = gapminder, 
            mapping = aes(x = gdpPercap, y=lifeExp, 
                          size = pop, color = country)) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy")

p

Gapminder returns

But add one thing

p_live <- p + transition_time(year) +
  labs(title = "Year: {frame_time}")
animate(p_live, renderer = ffmpeg_renderer())

But add one thing

Everything’s Available

p_live2 <- p + facet_wrap(~ continent) +
  transition_time(year) +
  labs(title = "Year: {frame_time}")
animate(p_live2, renderer = ffmpeg_renderer())

Everything’s Available

Lots of effects

p_live3 <- p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  view_follow(fixed_y = TRUE)

animate(p_live3, renderer = ffmpeg_renderer())

Lots of effects

Lots of effects

p_live4 <- p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_wake(wake_length = 0.1, alpha = FALSE)
animate(p_live4, renderer = ffmpeg_renderer())

Lots of effects

Another Example

p <- ggplot(data = airquality,
            mapping = aes(Day, Temp, group = Month, 
                          color = factor(Month))) +
  geom_line() +
  scale_color_viridis_d() +
  labs(x = "Day of Month", y = "Temperature") +
  theme(legend.position = "top")

p

Another Example

Another Example

p_live6 <- p + transition_reveal(Day)
animate(p_live6, renderer = ffmpeg_renderer())

Another Example

Another Example

p_live7 <- p + geom_point(aes(group = seq_along(Day))) +
  transition_reveal(Day)
animate(p_live7, renderer = ffmpeg_renderer())

Another Example