Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created April 3, 2023 17:15
Show Gist options
  • Save walkerke/5fa6044cf870327ce51b51b8127f6852 to your computer and use it in GitHub Desktop.
Save walkerke/5fa6044cf870327ce51b51b8127f6852 to your computer and use it in GitHub Desktop.
remotes::install_github("walkerke/tidycensus")
library(tidycensus)
library(tidyverse)
library(tigris)
options(tigris_use_cache = TRUE)
sysfonts::font_add_google("Roboto")
showtext::showtext_auto()
net_migration <- get_estimates(geography = "county",
variables = "RDOMESTICMIG",
year = 2022,
geometry = TRUE,
resolution = "20m") %>%
shift_geometry()
order = c("-15 and below", "-15 to -5", "-5 to 0", "0 to +5", "+5 to +15", "+15 and up")
net_migration <- net_migration %>%
mutate(groups = case_when(
value > 15 ~ "+15 and up",
value > 5 ~ "+5 to +15",
value > 0 ~ "0 to +5",
value > -5 ~ "-5 to 0",
value > -15 ~ "-15 to -5",
TRUE ~ "-15 and below"
)) %>%
mutate(groups = factor(groups, levels = order))
state_overlay <- states(
cb = TRUE,
resolution = "20m"
) %>%
filter(GEOID != "72") %>%
shift_geometry()
ggplot() +
geom_sf(data = net_migration, aes(fill = groups, color = groups), size = 0.1, color = "lightgrey") +
geom_sf(data = state_overlay, fill = NA, color = "black", size = 0.1) +
scale_fill_brewer(palette = "PuOr", direction = -1) +
coord_sf(datum = NA) +
theme_minimal(base_family = "Roboto", base_size = 22) +
labs(title = "Net domestic migration per 1000 residents by county, 2021-2022",
subtitle = "US Census Bureau 2022 Population Estimates",
fill = "Rate",
caption = "Data acquired with the R tidycensus package | @kyle_e_walker")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment