Mapping Zip Code vs. County shapefile in R

5.9k views Asked by At

I am trying to map the polygons for various geographic areas (i.e. county/zip codes). Based on what I have found at this blog I can easily accomplish this for counties.

library(rgdal)
library(rgeos)
library(leaflet)

url<-"http://www2.census.gov/geo/tiger/TIGER2010DP1/County_2010Census_DP1.zip"
downloaddir<-getwd()
destname<-"tiger_county.zip"
download.file(url, destname)
unzip(destname, exdir=downloaddir, junkpaths=TRUE)

filename<-list.files(downloaddir, pattern=".shp", full.names=FALSE)
filename<-gsub(".shp", "", filename)

# ----- Read in shapefile (NAD83 coordinate system)
# ----- this is a fairly big shapefile and takes 1 minute to read
dat<-readOGR(downloaddir, "County_2010Census_DP1") 

# ----- Create a subset of New York counties
subdat<-dat[substring(dat$GEOID10, 1, 2) == "36",]

# ----- Transform to EPSG 4326 - WGS84 (required)
subdat<-spTransform(subdat, CRS("+init=epsg:4326"))

# ----- save the data slot
subdat_data<-subdat@data[,c("GEOID10", "ALAND10")]

# ----- simplification yields a SpatialPolygons class
subdat<-gSimplify(subdat,tol=0.01, topologyPreserve=TRUE)

# ----- to write to geojson we need a SpatialPolygonsDataFrame
subdat<-SpatialPolygonsDataFrame(subdat, data=subdat_data)

leaflet() %>%
  addTiles() %>%
  addPolygons(data=subdat)

enter image description here

But if I run the exact same code with a different file for zip codes

url <- "http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_zcta510_500k.zip"

I get a completely different area of the country instead of New York.

enter image description here

Not sure if someone is more familiar with these datasets and these functions to explain why this difference happens?

1

There are 1 answers

0
cdeterman On BEST ANSWER

Given @hrbrmstr noticed that the zip codes returned are in fact zip codes in Alabama this made me second guess my previous assumption on the structure of the GEOID10 variable. I discovered this link which says that with the zcta files the GEOID10 variable is actually just the zip codes so it is not possible to filter the same as the county file.

I figured out another way to filter using the zip_codes dataset from the noncensus package. I then substituted the line

subdat<-dat[substring(dat$GEOID10, 1, 2) == "36",]

for

# get zip codes for New York
ny_zips <- zip_codes[zip_codes$state=="NY",]
subdat<-dat[dat$GEOID10 %in% ny_zips$zip,]

enter image description here