首页 > 解决方案 > 如何在 R 中按邮政编码创建肯塔基地图?

问题描述

假设我有一个包含肯塔基州邮政编码和介于 -1 和 1 之间的值的数据框。我想将它们绘制为肯塔基州的热图,其中 -1 代表渐变的最低颜色,1 代表最高颜色。

我怎样才能在 R 中做到这一点?我正在使用 R Studio 1.3.959(R 版本 3.6.3)。

标签: rdictionaryplotzipcode

解决方案


An option using tigris and sf. Note that this uses zip code tabulation areas, which are not a 1:1 match with zip codes. Zip codes follow streets and can't be made into sensible polygons: https://gis.stackexchange.com/a/2693/162034

library(tigris)
library(sf)

# download zipcode tabulation areas and state boundaries
zcta1 <- zctas(TRUE)
sts <- states(TRUE)

# subset zipcode data
zcta_ky <- st_intersection(zcta1, sts[sts$NAME == 'Kentucky', ])

# add random 0-1 field
zcta_ky$rand <- runif(nrow(zcta_ky))

#plot
plot(zcta_ky['rand'])


推荐阅读