r - 如何在 R 中创建邮政编码边界
问题描述
我正在尝试创建一个名为“社区”的地图,显示多个邮政编码的边界。我拥有的数据类似于下面。其中变量是社区的名称,数字是相应的邮政编码。
Tooele <- c('84074','84029')
NEUtahCo <- c('84003', '84004', '84042', '84062')
NWUtahCounty <- c('84005','84013','84043','84045')
我能够制作我想要使用的整个区域的地图
ggmap(get_map(location = c(lon=-111.9, lat= 40.7), zoom = 9))
附上我想要的图片。
解决方案
通过弄清楚 shapefile 以及它如何与您想要显示的 zip 匹配,您已经为此奠定了良好的基础。简单的功能(sf
_ggplot2
geom_sf
sf
我不确定您拥有的不同地区(县?)的名称是否重要,所以我只是将它们全部扔进了小标题中,并将它们绑定到一个标题中,utah_zips
. tigris
还增加了sf
支持,所以如果你设置class = "sf"
,你会得到一个sf
对象。为简单起见,我只是抽出您需要的列并简化其中一个名称。
library(tidyverse)
library(tigris)
library(ggmap)
Tooele <- c('84074','84029')
NEUtahCo <- c('84003', '84004', '84042', '84062')
NWUtahCounty <- c('84005','84013','84043','84045')
utah_zips <- bind_rows(
tibble(area = "Tooele", zip = Tooele),
tibble(area = "NEUtahCo", zip = NEUtahCo),
tibble(area = "NWUtahCounty", zip = NWUtahCounty)
)
zips_sf <- zctas(cb = T, starts_with = "84", class = "sf") %>%
select(zip = ZCTA5CE10, geometry)
head(zips_sf)
#> Simple feature collection with 6 features and 1 field
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -114.0504 ymin: 37.60461 xmax: -109.0485 ymax: 41.79228
#> epsg (SRID): 4269
#> proj4string: +proj=longlat +datum=NAD83 +no_defs
#> zip geometry
#> 37 84023 MULTIPOLYGON (((-109.5799 4...
#> 270 84631 MULTIPOLYGON (((-112.5315 3...
#> 271 84334 MULTIPOLYGON (((-112.1608 4...
#> 272 84714 MULTIPOLYGON (((-113.93 37....
#> 705 84728 MULTIPOLYGON (((-114.0495 3...
#> 706 84083 MULTIPOLYGON (((-114.0437 4...
然后你可以过滤sf
你需要的邮编——因为还有其他信息(县名),你可以使用连接在一个sf
数据框中获取所有内容:
utah_sf <- zips_sf %>%
inner_join(utah_zips, by = "zip")
head(utah_sf)
#> Simple feature collection with 6 features and 2 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -113.1234 ymin: 40.21758 xmax: -111.5677 ymax: 40.87196
#> epsg (SRID): 4269
#> proj4string: +proj=longlat +datum=NAD83 +no_defs
#> zip area geometry
#> 1 84029 Tooele MULTIPOLYGON (((-112.6292 4...
#> 2 84003 NEUtahCo MULTIPOLYGON (((-111.8497 4...
#> 3 84074 Tooele MULTIPOLYGON (((-112.4191 4...
#> 4 84004 NEUtahCo MULTIPOLYGON (((-111.8223 4...
#> 5 84062 NEUtahCo MULTIPOLYGON (((-111.7734 4...
#> 6 84013 NWUtahCounty MULTIPOLYGON (((-112.1564 4...
您已经确定了底图,并且由于ggmap
制作了ggplot
对象,您只需添加一个geom_sf
图层即可。技巧只是确保您声明您正在使用的数据,将其设置为不从 继承 aes ggmap
,并关闭coord_sf
.
basemap <- get_map(location = c(lon=-111.9, lat= 40.7), zoom = 9)
ggmap(basemap) +
geom_sf(aes(fill = zip), data = utah_sf, inherit.aes = F, size = 0, alpha = 0.6) +
coord_sf(ndiscr = F) +
theme(legend.position = "none")
您可能需要调整底图的位置,因为它会切断其中一个拉链。一种方法是使用st_bbox
获取 的边界框utah_sf
,然后使用它获取底图。
推荐阅读
- node.js - Express.JS req.body 正在将 _locals: [Object: null prototype] {} 记录到控制台
- java - 如何在 Anylogic 8.7 中使用变量作为从 darabase 检索数据的条件
- lua - 用鼠标按钮选择目标
- google-api - 日历列表。插入 404
- arrays - Powershell:当数组中只剩下一个对象时如何正确计算对象数组中的项目数
- python - 如何在 discord.py 中创建剪刀石头布命令
- laravel - 使用 Vue 发布对象以进行 Maatwerk Laravel Excel 导出
- r - R:来自 NHANES 的加权样本数据的调整比例
- r - TTR runSD 返回所有 NA
- unit-testing - 选项名称的 CMake 命名约定以生成带有单元测试的项目?