Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created December 22, 2024 20:56
Show Gist options
  • Save walkerke/fb42ad96b3afb7f4b7312ae8e83beb6c to your computer and use it in GitHub Desktop.
Save walkerke/fb42ad96b3afb7f4b7312ae8e83beb6c to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tidyverse)
library(scales)
state_pop <- get_estimates(
geography = "state",
variables = "POPESTIMATE",
vintage = 2024,
time_series = TRUE
)
ggplot(state_pop, aes(x = year, y = value, group = NAME)) +
geom_line(aes(color = NAME)) +
geom_point(aes(color = NAME)) +
facet_wrap(~ NAME, scales = "free_y", ncol = 6) +
theme_minimal() +
theme(
legend.position = "none",
strip.text = element_text(size = 8, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1, size = 6),
axis.text.y = element_text(size = 6),
axis.line.x = element_blank(),
panel.grid = element_blank(),
axis.line = element_line(color = "gray80"),
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 10, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
panel.spacing = unit(1, "lines"),
plot.margin = margin(10, 10, 10, 10)
) +
scale_color_viridis_d(option = "turbo") +
labs(
title = "Population Estimates by State (2020-2024)",
subtitle = "Showing individual trends for each state",
x = "Year",
y = "Population",
caption = "Data source: 2024 US Census Population Estimates, tidycensus R package\nPlease note: y-axes are not consistent across states"
) +
scale_x_continuous(breaks = 2020:2024) +
coord_cartesian(expand = TRUE, clip = "off") +
scale_y_continuous(
labels = label_number(scale = 1/1000000, suffix = "M", accuracy = 0.01),
breaks = function(x) c(min(x), max(x)),
expand = expansion(mult = c(0.1, 0.1))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment