Skip to content

Instantly share code, notes, and snippets.

@walkerke
Last active May 19, 2021 12:49
Show Gist options
  • Save walkerke/dd39d46ddc56331c15460e960b3e22f8 to your computer and use it in GitHub Desktop.
Save walkerke/dd39d46ddc56331c15460e960b3e22f8 to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tidyverse)
library(tigris)
library(sf)
options(tigris_use_cache = TRUE)
us_states <- c(state.abb, "DC", "PR")
# To get the map to work correctly,
# empty geometries must be removed along with
# the archipelago of uninhabited islands NW of
# Hawaii
us_tracts <- get_acs(
geography = "tract",
variables = "DP05_0077P",
state = us_states,
year = 2019,
geometry = TRUE
) %>%
filter(GEOID != "15003981200",
!st_is_empty(.)) %>%
shift_geometry()
state_overlay <- states(
cb = TRUE,
resolution = "20m",
year = 2019
) %>%
shift_geometry()
ggplot() +
geom_sf(data = us_tracts,
aes(fill = estimate),
color = NA) +
geom_sf(data = state_overlay,
fill = NA,
color = "black",
size = 0.2) +
scale_fill_distiller(palette = "YlGnBu",
labels = function(x) paste0(x, "%"),
direction = 1) +
theme_void(base_size = 16) +
labs(fill = "ACS estimate ",
title = " Percent non-Hispanic white by Census tract",
subtitle = " 2015-2019 ACS",
caption = "Note: Alaska, Hawaii, and Puerto Rico are shifted and are not to scale.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment