在R中映射邮政编码与郡形状文件

在R中映射邮政编码与郡形状文件,r,leaflet,tiger-lines,R,Leaflet,Tiger Lines,我正在尝试映射不同地理区域(即县/邮政编码)的多边形。根据我在的发现,我可以很容易地为各县完成这项工作 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.fil

我正在尝试映射不同地理区域(即县/邮政编码)的多边形。根据我在的发现,我可以很容易地为各县完成这项工作

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)
库(rgdal)
图书馆(rgeos)
图书馆(单张)

urlgive@hrbrmstr注意到返回的邮政编码实际上是阿拉巴马州的邮政编码。这让我对之前关于
GEOID10
变量结构的假设进行了二次猜测。我发现zcta文件中的
GEOID10
变量实际上只是邮政编码,因此无法过滤与县文件相同的内容

我从
nonccensus
包中找到了另一种使用
zip\u-code
数据集进行过滤的方法。然后我换了那条线

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

subdat我想知道重新投影时是否存在问题?第二个shapefile中的条目0具有
ZCTA5CE10(字符串)=36426
。那是阿拉巴马州,地图上的绘图区域是阿拉巴马州。@hrbrmstr我的印象是
GEOID10
中的前两位数字对应于fips州代码(NY=36)。
subdat<-dat[substring(dat$GEOID10, 1, 2) == "36",]
# get zip codes for New York
ny_zips <- zip_codes[zip_codes$state=="NY",]
subdat<-dat[dat$GEOID10 %in% ny_zips$zip,]