Elements of Data Science SDS 322E

H. Sherry Zhang
Department of Statistics and Data Sciences
The University of Texas at Austin

Learning objectives

We will learn to create animated and interactive graphics using two R packages:

  • gganimate: to create animated graphics in R
  • plotly: to create interactive web-based graphics

In both cases, we will start from a static ggplot2 plot and extend it to an animation or interactive plot.

Remember the gapminder animation?

Start from a static plot

p1 <- ggplot(gapminder, aes(x = gdpPercap, lifeExp, 
                            size = pop, colour = continent)) +
  geom_point(show.legend = FALSE)
p1

Add some color

scales::show_col(gapminder::continent_colors)

Add some color

p2 <- p1 +
  scale_color_manual(values = gapminder::continent_colors) +
  scale_x_log10(
    breaks = c(500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000),
    labels = c("500", "1000", "2000", "4000", "8k", "16k", "32k", "64k", "128k")
  ) +
  scale_y_continuous(breaks = seq(10, 90, by = 10))

p2

Animation with gganimate

The gganimate package extends the ggplot2 package for creating animated graphics.

  • The core idea is to specify how the plot should change over time (or other dimensions) using special gganimate functions: transition_*()
  • The most common function is transition_time(), which creates frames based on a continuous variable (e.g., time).

gganimate creates animations by interpolating between the frames defined by the transitions and then rendering them as a series of images that can be played back in sequence: animate(renderer = ...)

Animation with gganimate

p2 + transition_time(year)

Animation with gganimate

p2 + geom_text(aes(label = country), vjust = 1.5, size = 3) + 
  transition_time(year)

Your time

  1. We may also want to add a label to indicate which year is the current frame. Read the documentation of transition_time() and produce the following plot.
  1. To save the animation as a GIF file, use the anim_save() function. Read the documentation and save your animation as a GIF file.

Solution

p2 + transition_time(year) +
  labs(title = 'Year: {frame_time}', 
       x = 'GDP per capita', y = 'life expectancy')
p3 <- p2 + geom_text(aes(label = country)) + transition_time(year)
anim_save(p3, filename = "animation/anim/gapminder_animation.gif",
           width = 500, height = 300, nframes = 100, fps = 10)

Animation with plotly

The plotly package is powered by the plotly.js JavaScript library, which provides a wide range of interactive chart types and features.

  • The ggplotly() function converts a static ggplot2 plot into an interactive plotly plot.
  • To create animations, we can map a variable to the frame aesthetic in geom_point(), which will create frames based on the values of that variable.

The interactive plot allows users to explore the data by zooming, panning, and hovering over points (tooltips) to see additional information.

Animation with plotly

library(plotly)
gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10()
ggplotly(gg)

Your time

  1. Use the lasso or box select tool to select a group of countries in the interactive plot above and observe how they change over time.

  2. Save the interactive plot as an HTML file using the htmlwidgets::saveWidget() function.

Solution

library(htmlwidgets)
saveWidget(ggplotly(gg), "animation/anim/gapminder_interactive.html")

Highlights with plotly

penguins |> 
  GGally::ggpairs(aes(color = species), columns = 3:6) %>%
  ggplotly() %>%
  highlight("plotly_selected")

Your time

In the interactive plot above, use the lasso or box select tool to select a group of points and observe how they are highlighted across all panels. What are other gestures you can use to highlight points in plotly? Find out more in the documentation of highlight() and try them out!