I'm trying to use the tigris package in R to make a map of Douglas County, Colorado. The goal is to show Douglas County divided by the zip codes inside of it, and to have each zip code shaded based on a value. The variables I have right now in my dataset are: Zip code, town, and lead level (continuous variable).
library(tigris)
library(sf)
douglas_zips <- zctas(cb=TRUE, starts_with= c("80108","80109", "80104", "80116", "80126", "80129", "80130", "80118", "80124", "80131", "80134", "80138", "80125", "80135"))
plot(douglas_zips)
When I do this, I get this strange plot: zip code map
Any thoughts or ideas of where to go from here?
The information from Spacedman is correct, but Matt is asking how to execute a data join between the douglas_zips feature and a separate table containing a list of lead levels.
The 'join' is an essential function in GIS and relational databases broadly, so it's a critical skill.
We want to take our douglas_zips feature and match all the records from the lead_levels table by the zipcode, adding the columns from lead_levels to douglas_zips. We can use merge() to accomplish that.
https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/merge
NOTE: specify either 'cb=TRUE, year=2020' or 'cb=FALSE' (the default) for ZCTAs from tigris.
The result will add two columns to douglas_zips: "town" and "lead_level".
From there, we can plot the lead_level variable as Spacedman explained: