Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created December 26, 2024 15:08
Show Gist options
  • Save walkerke/cfcfe22ca6cfec5d7a8b24a07b8fe3bb to your computer and use it in GitHub Desktop.
Save walkerke/cfcfe22ca6cfec5d7a8b24a07b8fe3bb to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tidyverse)
library(geofacet)
library(scales)
state_birth_rates <- get_estimates(
geography = "state",
variables = "RBIRTH",
vintage = 2024,
time_series = TRUE
)
ggplot(state_birth_rates, aes(x = year, y = value, group = NAME)) +
geom_line(color = "navy") +
geom_point(color = "navy") +
facet_geo(~ NAME, scales = "free_y") +
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(0.1, "lines"),
plot.margin = margin(10, 10, 10, 10)
) +
labs(
title = "Crude Birth Rate Trends by State (2021-2024)",
subtitle = "Data source: 2024 US Census Population Estimates, tidycensus R package",
x = "Year",
y = "Birth Rate",
caption = "Reflects births per 1000 residents for the 12 months prior to June 30 of a given year\nPlease note: y-axes are not consistent across states"
) +
scale_x_continuous(breaks = 2021:2024) +
coord_cartesian(expand = TRUE, clip = "off") +
scale_y_continuous(
labels = label_number(accuracy = 0.1),
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