Lecture 20
Cornell University
INFO 3312/5312 - Spring 2025
April 10, 2025
Overview first, zoom and filter, then details on demand
ggplotly()
converts {ggplot2} plots to {plotly} plotsstatic_plot <- ggplot(
data = partisan_names,
mapping = aes(
x = part_diff,
y = fct_rev(year),
color = outcome
)
) +
geom_quasirandom() +
scale_x_continuous(labels = label_percent(style_positive = "plus")) +
scale_color_manual(values = c(dem, rep), guide = "none") +
labs(
title = "The most popular names have gotten more polarized",
x = "Partisan gap",
y = NULL,
caption = '"Blue" and "red" state designations are based on the 2024 presidential election results.\nOnly names assigned at least 100 times in each year were included.\n\nSource: Social Security Administration'
)
static_plot
ggplotly()
theme(legend.position = "none")
)static_plot_tooltip <- ggplot(
data = partisan_names,
mapping = aes(
x = part_diff,
y = fct_rev(year),
color = outcome
)
) +
geom_quasirandom(mapping = aes(text = name)) +
scale_x_continuous(labels = label_percent(style_positive = "plus")) +
scale_color_manual(values = c(dem, rep), guide = "none") +
labs(
title = "The most popular names have gotten more polarized",
x = "Partisan gap",
y = NULL,
) +
theme(legend.position = "none")
static_plot_tooltip
Create a custom tooltip using the format:
Year: Baby name
+XX% R/D
Generate a new column using mutate()
with the required HTML character string
str_glue()
to combine character strings with data values<br>
is HTML syntax for a line breaklabel_percent()
function to format numbers as percentspartisan_names <- partisan_names |>
mutate(
outcome = if_else(outcome == "Trump", "R", "D"),
abs_part_diff = abs(part_diff),
pct_label = label_percent(accuracy = 1, style_positive = 'plus')(abs_part_diff),
fancy_label = str_glue("{year}: {name}<br>{pct_label} {outcome}")
)
partisan_names |>
select(year, part_diff, outcome, pct_label, fancy_label)
# A tibble: 200 × 5
year part_diff outcome pct_label fancy_label
<fct> <dbl> <chr> <chr> <glue>
1 1983 0.884 R +88% 1983: Kendrick<br>+88% R
2 1983 0.864 R +86% 1983: Trey<br>+86% R
3 1983 0.854 R +85% 1983: Rodrick<br>+85% R
4 1983 0.833 R +83% 1983: Ashlea<br>+83% R
5 1983 0.780 R +78% 1983: Tosha<br>+78% R
6 1983 0.773 R +77% 1983: Misti<br>+77% R
7 1983 0.767 R +77% 1983: Latoria<br>+77% R
8 1983 0.753 R +75% 1983: Jackie<br>+75% R
9 1983 0.740 R +74% 1983: Demarcus<br>+74% R
10 1983 0.737 R +74% 1983: Angelia<br>+74% R
# ℹ 190 more rows
static_plot_tooltip_fancy <- ggplot(
data = partisan_names,
mapping = aes(
x = part_diff,
y = fct_rev(year),
color = outcome
)
) +
geom_quasirandom(mapping = aes(text = fancy_label)) +
scale_x_continuous(labels = label_percent(style_positive = "plus")) +
scale_color_manual(values = c(dem, rep), guide = "none") +
labs(
title = "The most popular names have gotten more polarized",
x = "Partisan gap",
y = NULL,
) +
theme(legend.position = "none")
ggplotly(static_plot_tooltip_fancy,
tooltip = "text"
)
ae-19
Instructions
ae-19
(repo name will be suffixed with your GitHub name).renv::restore()
to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.Or take INFO 3300/5100 to learn to do this from scratch using D3.js
ggplotly()
to convert {ggplot2} plots to an interactive form