stopifnot("plot_data has 0 rows" = nrow(plot_data) > 0)
# Get endpoint labels for ggrepel
endpoints <- plot_data %>%
group_by(label) %>%
slice_max(year_ended_june, n = 1) %>%
ungroup()
# Palette: MetBrewer::Austria — earthy, distinct, pastoral
pal <- paletteer::paletteer_d("MetBrewer::Austria", n = 4)
# Custom theme
theme_tt <- theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", size = 17, lineheight = 1.15, margin = margin(b = 6)),
plot.subtitle = element_text(size = 12, color = "#555555", margin = margin(b = 16)),
plot.caption = element_text(size = 9, color = "#888888", margin = margin(t = 12)),
plot.background = element_rect(fill = "#faf8f4", color = NA),
panel.background = element_rect(fill = "#faf8f4", color = NA),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_line(color = "#e0dbd0", linewidth = 0.4),
axis.title.x = element_text(color = "#666666", margin = margin(t = 8)),
axis.title.y = element_text(color = "#666666", margin = margin(r = 8)),
axis.text = element_text(color = "#555555"),
legend.position = "none",
plot.margin = margin(16, 60, 12, 16)
)
p <- ggplot2::ggplot(
plot_data,
ggplot2::aes(x = year_ended_june, y = index, color = label)
) +
# Reference line at 100 (= 1982 baseline)
ggplot2::geom_hline(yintercept = 100, linetype = "dashed", color = "#aaaaaa", linewidth = 0.6) +
# Vertical line at 1984 (subsidy removal)
ggplot2::geom_vline(xintercept = 1984, linetype = "dotted", color = "#cc6633", linewidth = 0.7, alpha = 0.8) +
# Main lines
ggplot2::geom_line(linewidth = 1.2, lineend = "round") +
# Endpoint labels
ggrepel::geom_text_repel(
data = endpoints,
ggplot2::aes(label = paste0(label, "\n", round(index), "")),
direction = "y",
hjust = 0,
nudge_x = 1.5,
size = 3.6,
fontface = "bold",
segment.color = "#cccccc",
segment.size = 0.3,
box.padding = 0.3,
min.segment.length = 0.2
) +
# Annotation: subsidy removal
ggplot2::annotate(
"text",
x = 1984.5, y = 5,
label = "1984: Farm\nsubsidies\nremoved",
hjust = 0, vjust = 0,
size = 3, color = "#cc6633", fontface = "italic"
) +
# Annotation: sheep peak
ggplot2::annotate(
"point",
x = 1982, y = 100,
shape = 21, size = 3.5, fill = pal[1], color = "white", stroke = 1.2
) +
ggplot2::annotate(
"text",
x = 1981, y = 104,
label = "Sheep peak:\n70.3 million (1982)",
hjust = 1, size = 3, color = "#555555", fontface = "italic"
) +
paletteer::scale_color_paletteer_d("MetBrewer::Austria") +
ggplot2::scale_x_continuous(
breaks = seq(1975, 2025, by = 5),
expand = ggplot2::expansion(mult = c(0.01, 0.12))
) +
ggplot2::scale_y_continuous(
labels = function(x) paste0(x),
breaks = seq(0, 200, by = 25)
) +
ggplot2::labs(
title = "The Sheep That Weren't There",
subtitle = "New Zealand livestock indexed to 1982 (= 100). Sheep numbers have fallen\nto one-third of their peak; dairy cattle have grown by 80%.",
x = "Year (ended June)",
y = "Index (1982 = 100)",
caption = "Source: StatsNZ via TidyTuesday 2026-02-17 · Palette: MetBrewer::Austria"
) +
theme_tt
p