Skip to content

Instantly share code, notes, and snippets.

@cararthompson
Last active September 30, 2025 16:38
Show Gist options
  • Select an option

  • Save cararthompson/466115436631411a7298f9264d175dac to your computer and use it in GitHub Desktop.

Select an option

Save cararthompson/466115436631411a7298f9264d175dac to your computer and use it in GitHub Desktop.
# Baby names
# Get hold of the data
library(ggplot2)
install.packages("babynames")
# Make a graph ----
# |- Pick the names you want to investigate ----
names_to_visualise <- c("Charlotte", "Hannah", "Christine")
# |- Make a basic graph ----
babynames::babynames |>
dplyr::filter(name %in% names_to_visualise, sex == "F") |>
ggplot(aes(x = year, y = n, group = name, color = name)) +
geom_line() +
geom_point()
# Change the colours ----
name_colours <- c(
"Charlotte" = "pink",
"Hannah" = "orange",
"Christine" = "#2c3d4f"
)
babynames::babynames |>
dplyr::filter(name %in% names_to_visualise, sex == "F") |>
ggplot(aes(x = year, y = n, group = name, color = name)) +
geom_line() +
geom_point() +
scale_colour_manual(values = name_colours) +
labs(title = "Three names over time") +
theme_minimal() +
theme(axis.title = element_blank())
# Add theme_afbi() ----
babynames::babynames |>
dplyr::filter(name %in% names_to_visualise, sex == "F") |>
ggplot(aes(x = year, y = n)) +
geom_line(aes(group = name, color = name)) +
geom_point(aes(group = name, color = name)) +
scale_colour_manual(values = name_colours) +
labs(title = "Three names over time") +
theme_afbi() +
theme(axis.title = element_blank())
# Add an annotation -----
babynames::babynames |>
dplyr::filter(name %in% names_to_visualise, sex == "F") |>
ggplot(aes(x = year, y = n)) +
geom_line(aes(group = name, color = name)) +
geom_point(aes(group = name, color = name)) +
scale_colour_manual(values = name_colours) +
geom_vline(xintercept = 2000, linetype = 2) +
ggtext::geom_textbox(
data = data.frame(),
aes(
x = 1998,
y = 22500,
label = "The name Hannah was at peak popularity in 2000"
),
hjust = 1,
halign = 1,
family = "Open Sans",
fill = NA,
box.colour = NA,
width = unit(12, "lines")
) +
labs(title = "Three names over time") +
theme_afbi() +
theme(axis.title = element_blank())
# Add line-end annotations ----
babynames::babynames |>
dplyr::filter(name %in% names_to_visualise, sex == "F") |>
ggplot(aes(x = year, y = n)) +
geom_line(aes(group = name, color = name)) +
geom_point(aes(group = name, color = name)) +
geom_vline(xintercept = 2000, linetype = 2) +
ggtext::geom_textbox(
data = data.frame(),
aes(
x = 1998,
y = 22500,
label = "The name Hannah was at peak popularity in 2000"
),
hjust = 1,
halign = 1,
family = "Open Sans",
fill = NA,
box.colour = NA,
width = unit(12, "lines")
) +
ggtext::geom_textbox(
data = dplyr::filter(
babynames::babynames,
name %in% names_to_visualise,
sex == "F",
year == max(year)
),
aes(label = name),
hjust = 0,
halign = 0,
box.colour = NA,
fill = NA,
family = "Open Sans",
fontface = "bold"
) +
scale_colour_manual(values = name_colours) +
scale_x_continuous(
breaks = c(1900, 1950, 2000),
expand = expansion(c(0.05, 0.3))
) +
labs(title = "Three names over time") +
theme_afbi() +
theme(axis.title = element_blank(), legend.position = "none")
# Make interactive ----
interactive_plot <- babynames::babynames |>
dplyr::filter(name %in% names_to_visualise, sex == "F") |>
ggplot(aes(x = year, y = n)) +
geom_line(aes(group = name, color = name)) +
ggiraph::geom_point_interactive(aes(
group = name,
color = name,
tooltip = paste0(name, "s born in ", year, ": ", format(n, big.mark = ","))
)) +
geom_vline(xintercept = 2000, linetype = 2) +
ggtext::geom_textbox(
data = data.frame(),
aes(
x = 1998,
y = 22500,
label = "The name Hannah was at peak popularity in 2000"
),
hjust = 1,
halign = 1,
family = "Open Sans",
fill = NA,
box.colour = NA,
width = unit(12, "lines")
) +
ggtext::geom_textbox(
data = dplyr::filter(
babynames::babynames,
name %in% names_to_visualise,
sex == "F",
year == max(year)
),
aes(label = name),
hjust = 0,
halign = 0,
box.colour = NA,
fill = NA,
family = "Open Sans",
fontface = "bold"
) +
scale_colour_manual(values = name_colours) +
scale_x_continuous(
breaks = c(1900, 1950, 2000),
expand = expansion(c(0.05, 0.3))
) +
labs(title = "Three names over time") +
theme_afbi() +
theme(axis.title = element_blank(), legend.position = "none")
ggiraph::girafe(
ggobj = interactive_plot,
options = list(
ggiraph::opts_tooltip(css = afbi_tooltip_css),
ggiraph::opts_hover(css = "stroke:#30373b"),
ggiraph::opts_hover_inv(css = "opacity:0.5")
),
# Play around with these dimensions to get the desired aspect ratio
height_svg = 9,
width_svg = 6
)
# Exporting graphs
ggsave(plot = my_plot_name,
filename = "the-path/filename.png", height = 9, width = 6, dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment