US Census Spatial Data
US Census spatial data sandbox
Part 1
Run the code below to compare various legal and US Census geographies. Then, consider the following questions:
- To what do boundaries coincide (E.g., MSAs align with what?)?
- Do any geographies seem malformed or weirdly shaped?
- How many block groups are there per tract?
- Does anything surprise you?
library(tigris)
library(leaflet)
library(dplyr)
library(sf)
ec_block_groups <- block_groups(state = "55", county = "Eau Claire")
ec_census_tracts <- tracts(state = "55", county = "Eau Claire")
cbsa <- core_based_statistical_areas(year=2020)
csa <- combined_statistical_areas(year=2020)
leaflet() %>%
addProviderTiles(provider = providers$CartoDB.Positron) %>%
addPolygons(data = ec_block_groups, group = "Block Groups", fillColor = "yellow", label = ~NAMELSAD, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addPolygons(data = ec_census_tracts, group = "Tracts", fillColor = "blue", label = ~NAMELSAD, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addPolygons(data = cbsa, group = "CBSA", fillColor = "green", label = ~NAMELSAD, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addPolygons(data = csa, group = "CSA", fillColor = "red", label = ~NAMELSAD, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addLayersControl(overlayGroups = c("Block Groups", "Tracts", "CBSA", "CSA"))
Part 2
Run the code below to compare place (i.e. municipality) boundaries to US Census Urban Areas:
library(sf)
library(tigris)
library(leaflet)
library(dplyr)
wi_counties <- counties("55", cb = TRUE)
wi_places <- places("55")
us_urban <- urban_areas(year = 2023)
intersect_index <- st_intersects(us_urban,
wi_counties %>% st_union()) %>%
as.numeric()
wi_urban <- us_urban[which(!is.na(intersect_index)),]
leaflet() %>%
addProviderTiles(provider = providers$CartoDB.Positron) %>%
addPolygons(data = wi_counties, group = "Counties", fillColor = "cyan", label = ~NAME, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addPolygons(data = wi_places, group = "Places", fillColor = "magenta", label = ~NAME, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addPolygons(data = wi_urban, group = "Urban Areas", fillColor = "yellow", label = ~NAME20, weight = 1, smoothFactor = 0.5, color = "#444444") %>%
addLayersControl(overlayGroups = c("Counties", "Places", "Urban Areas"))