For your information:
- For brevity, zipcode layers are not included in the interactive maps.
- The list of counties are avialble upon request to ylee366 (at) gatech (dot) edu
library(tidyverse)
library(sf)
library(tigris)
library(tmap)
options(tigris_use_cache = TRUE)
tigris_cache_dir(paste0(getwd(), "/tigris"))
readRenviron('~/.Renviron')
- Stpe 1: Process the Core Based Statistical Area boundaries.
cbsa <- core_based_statistical_areas() %>% st_as_sf() %>% st_transform(crs = 2163)
cbsa_keyword <- c("San Francisco", "Los Angeles", "Sacramento", "Seattle",
"Boston", "Washington-Arlington-Alexandria, DC-VA-MD-WV", "Salt Lake City", "Kansas City",
"New York")
cbsa_list <- map(cbsa_keyword, ~cbsa$NAME[c(1:945)[grepl(., cbsa$NAME)]]) %>% unlist()
cbsa_selected <- cbsa %>%
filter(NAME %in% cbsa_list) %>%
select(CSAFP, CBSAFP, CBSA.NAME = NAME)
- Stpe 2: Process the list of counties inside the selected Core Based Statistical Areas.
cnty <- counties() %>% st_as_sf() %>% st_transform(crs = 2163)
cnty_selected <- cnty %>%
select(STATEFP, COUNTYFP, CNTY.NAME = NAME) %>%
st_join(cbsa_selected, join = st_covered_by) %>%
filter(is.na(CSAFP) == FALSE)
- Stpe 3: Process the list of Zipcode areas that overlap the selected Core Based Statistical Areas.
zipcode <- zctas() %>% st_as_sf() %>% st_transform(crs = 2163)
zipcode_selected <- zipcode %>%
select(ZCTA5CE10) %>%
st_join(cbsa_selected, join = st_covered_by) %>%
filter(is.na(CSAFP) == FALSE)
# write_rds(zipcode_selected, "zipcode_selected.rds")
# zipcode_selected <- read_rds("zipcode_selected.rds")
- Stpe 4: Create interactive maps.
tmap_mode("view")
map_cbsa <- map(
cbsa_selected$CBSAFP,
function(x) tm_basemap("Stamen.TonerLite") +
#tm_shape(cnty_selected %>% filter(CBSAFP == x)) +
#tm_fill(col = "grey", alpha = 0.5) +
#tm_borders(col = "black", lwd = 1, alpha = 0.5) +
#tm_text("CNTY.NAME", size = 1.2) +
tm_shape(zipcode_selected %>% filter(CBSAFP == x)) +
tm_fill(col = "grey", alpha = 0.5) +
tm_borders(col = "white", lwd = 1.5, alpha = 0.5) +
tm_shape(cbsa_selected %>% filter(CBSAFP == x)) +
tm_borders(col = "red")
)
- For data limiation per page, only the Sacrameto CBSA is shown here.
# map_cbsa[[9]] # Boston
# map_cbsa[[8]] # Kansas City
# map_cbsa[[7]] # Los Angeles
# map_cbsa[[6]] # NY
map_cbsa[[2]] # Sacramento
# map_cbsa[[3]] # Salt Lake City
# map_cbsa[[1]] # San Francisco
# map_cbsa[[4]] # Seattle
# map_cbsa[[5]] # Washington-Arlington-Alexandria