highlight_colors <- c(
"Pakistan" = "#B31B1B",
"China" = "#222222",
"United States" = "gray45"
)
solar_trend_hl <- solar_trend |>
mutate(
highlight = if_else(country %in% names(highlight_colors), country, "Other")
)
solar_labels <- solar_trend_hl |>
filter(year == max(year), highlight != "Other") |>
mutate(country = if_else(country == "United States", "US", country))
solar_mobile <- ggplot(
data = solar_trend_hl,
mapping = aes(x = year, y = solar, group = country)
) +
# context: every other country in light gray
geom_line(
data = filter(solar_trend_hl, highlight == "Other"),
color = "gray80",
linewidth = 0.6
) +
# focus: the three countries in the story
geom_line(
data = filter(solar_trend_hl, highlight != "Other"),
mapping = aes(color = highlight),
linewidth = 1.4
) +
# direct labels instead of a legend
geom_text(
data = solar_labels,
mapping = aes(label = country, color = highlight),
hjust = 0,
nudge_x = 0.4,
fontface = "bold",
size = 3.9
) +
scale_color_manual(values = highlight_colors, guide = "none") +
scale_x_continuous(
breaks = c(2010, 2015, 2020, 2025),
limits = c(2010, 2029.5)
) +
scale_y_continuous(labels = label_percent(scale = 1)) +
labs(
title = "Pakistan's solar boom",
subtitle = "Share of electricity from solar. Pakistan went\nfrom under 1% to 19% in seven years.",
x = NULL,
y = NULL,
caption = "Gray lines: 9 other countries\nSource: Our World in Data"
) +
theme_mobile()
ggsave(
filename = file.path(mobile_dir, "solar-mobile.png"),
plot = solar_mobile,
width = 4,
height = 5,
dpi = 270
)