Skip to content

Instantly share code, notes, and snippets.

@cararthompson
Created June 8, 2026 16:48
Show Gist options
  • Select an option

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

Select an option

Save cararthompson/a16f616f312ccdac5e8c6e0072132f1d to your computer and use it in GitHub Desktop.
Line and dot graph
# Baby names
# Get hold of the data
library(ggplot2)
#install.packages("babynames")
babynames::babynames |>
dplyr::filter(year == "1900") |>
dplyr::arrange(-n) |>
head(10) |>
dplyr::arrange(n) |>
dplyr::mutate(name = factor(name, levels = unique(name))) |>
ggplot() +
geom_col(aes(y = name, x = n, fill = sex), stat = "identity") +
theme_minimal() +
theme(panel.grid.major.y = element_blank())
# Step 1: 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(
"Hannah" = "#E59608", # <- hero colour
"Christine" = "#011451", # <- Glasgow blue
"Charlotte" = monochromeR::generate_palette(
"#011451",
"go_lighter",
n_colours = 3
)[2] # <- colour variant
)
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_glasgow() ----
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_glasgow() +
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,
# We need to specify the family here because ggtext::geom_textbox isn't native to ggplot
family = "Noto Sans",
# Same here, to determine the text colour
colour = "#343536",
fill = NA,
box.colour = NA,
width = unit(12, "lines")
) +
labs(title = "Three names over time") +
theme_glasgow() +
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 = "Noto Sans",
colour = "#343536",
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 = "Noto Sans",
colour = "#343536",
fontface = "bold"
) +
scale_colour_manual(values = name_colours) +
scale_x_continuous(
breaks = c(1900, 1950, 2000),
expand = expansion(c(0.05, 0.2))
) +
# Formatting the y axis labels
scale_y_continuous(labels = function(x) format(x, big.mark = ",")) +
labs(title = "Three names over time") +
theme_glasgow() +
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 = "Noto Sans",
colour = "#343536",
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 = "Noto Sans",
colour = "#343536",
fontface = "bold"
) +
scale_colour_manual(values = name_colours) +
scale_x_continuous(
breaks = c(1900, 1950, 2000),
expand = expansion(c(0.05, 0.2))
) +
scale_y_continuous(labels = function(x) format(x, big.mark = ",")) +
labs(title = "Three names over time") +
theme_glasgow() +
theme(axis.title = element_blank(), legend.position = "none")
ggiraph::girafe(
ggobj = interactive_plot,
options = list(
ggiraph::opts_tooltip(css = glasgow_tooltip_css)
),
height_svg = 6,
width_svg = 9
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment