Exploring the Wide World of ggplot2 Extensions

Eric R. Scott
Kristina Riemer

Learning Objectives

  • Understand where to find packages that extend ggplot2
  • Make animated plots with gganimate
  • Interactively create plots with esquisse
  • Convert ggplot2 plots to interactive plotly plots
  • Label data with ggrepel

Packages

library(ggplot2)
library(palmerpenguins)

Extensions to ggplot2

There are many kinds of extensions to ggplot2, but many of them can be put into one of two “bins”

  1. Packages that add new layers
  2. Packages that wrap multiple layers for convenience

Packages that extend ggplot2

Some packages add new layers or themes that can be combined with standard ggplot2 functions.

library(ggbeeswarm)
ggplot(penguins, aes(x = species, y = body_mass_g)) +
  geom_boxplot() +
  geom_beeswarm()

Packages that wrap ggplot2

Other packages provide convenience “all-in-one” functions that combine multiple layers for you.

library(ggpubr)
ggstripchart(penguins, x = "species", y = "body_mass_g",
             add = "boxplot") +
  scale_y_continuous("Body Mass (g)", n.breaks = 10)

Finding Extensions

Package Demos

gganimate

library(gganimate)

ggplot(penguins, aes(x = body_mass_g, y = flipper_length_mm)) +
  geom_point() +
  transition_time(year) + 
  labs(title = "Year: {frame_time}")

gganimate

ggplot(penguins, aes(x = body_mass_g, y = flipper_length_mm)) +
  geom_point() +
  transition_states(year, transition_length = 0.5) +
  labs(title = "Year: {closest_state}") +
  shadow_wake(wake_length = 0.1)
anim_save("~/Desktop/test_anim.gif")

plotly

library(plotly)
  • plotly R package is interface to a JavaScript library for making interactive data visualizations

  • Only important to know that because if you search for plotly help, sometimes you’ll get JavaScript code examples.

  • plotly kind of uses the grammar of graphics, but building a plot from scratch is tricky

  • ggplotly() transforms ggplot objects into interactive plotly plots. 90% of the time it gets you 90% of the way there

Basic interactive plot with plotly

p <- 
  ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  geom_point() +
  labs(x = "Bill length (mm)", y = "Bill depth (mm)", color = "Penguin Species") +
  theme_bw()

ggplotly(p)

Customize tooltip

You can customize the info displayed in the tooltip with ggplotly(). Give it additional aesthetic text to include something only as a tooltip.

p2 <- 
  ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  #Ignore unknown aesthetics warning
  geom_point(aes(text = sex)) +
  labs(x = "Bill length (mm)", y = "Bill depth (mm)", color = "Penguin Species") +
  theme_bw()

ggplotly(p2, tooltip = c("text", "bill_depth_mm", "bill_length_mm"))

Warning

Output not shown because currently plotly does not play nicely with Quarto revealjs slides (how this slide deck was made)

Animate plots with plotly

You can use ggplotly() to make interactive animated plots by using the frame aesthetic

library(gapminder) #dataset
p3 <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10()

ggplotly(p3)

Warning

Output not shown because currently plotly does not play nicely with Quarto revealjs slides (how this slide deck was made)

esquisse

library(dplyr)
library(esquisse)

penguins <- 
  penguins %>%
  na.omit()
esquisser(penguins)

ggrepel

The ggrepel package is helpful for directly labeling plots, especially when labels would otherwise overlap.

geom_text()

geom_text_repel()

Getting Help

Anything we missed?

Got a ggplot2 question we didn’t cover in this workshop series? Let’s figure it out together!