gpt4 book ai didi

从 R 中的美国县级 map 中删除五大湖

转载 作者:行者123 更新时间:2023-12-04 10:45:36 24 4
gpt4 key购买 nike

我正在使用 R 绘制美国县级 map 。我从 GADM 下载了美国的 shapefile .县级形状文件为“gadm36_USA_2.shp”。然后我使用下面的代码绘制 map :

library(sf)
library(tidyverse)

us2 <- st_read("<Path>\\gadm36_USA_2.shp")

mainland2 <- ggplot(data = us2) +
geom_sf(aes(fill = NAME_2), size = 0.4, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) + guides(fill = F)

五大湖区(用红色箭头表示)已绘制出来,而不是留空: enter image description here

我要的是如下图,五大湖区留空: enter image description here

我如何从“gadm36_USA_2.shp”中识别出哪些行对应于五大湖地区,以便我可以删除它们?

我知道除了 GADM 之外,还有其他方法可以获取 shapefile。我相信 GADM 是提供全局边界的极好资源。我希望借此机会更好地了解从 GADM 下载的数据。

当然,欢迎使用其他方法获取美国县级边界数据。我注意到 USAboundaries 包还提供国家、州和县级边界,但我在安装相关的 USABoundariesData 包时遇到了困难。欢迎任何以 GADM 的 shapefile 以外的方式绘制美国县的想法。谢谢。

最佳答案

一种方法是删除现有记录中标记有 Lake 的每个特征(目前有 13 个特征)。首先,您需要在属性表中找到湖泊名称,如下所示:

# retrieving the name of lakes and excluding them from the sf 

all.names = us2$NAME_2
patterns = c("Lake", "lake")

lakes.name <- unique(grep(paste(patterns, collapse="|"), all.names, value=TRUE, ignore.case = TRUE))
#[1] "Lake and Peninsula" "Lake" "Bear Lake" "Lake Michigan" "Lake Hurron" "Lake St. Clair"
#[7] "Lake Superior" "Lake of the Woods" "Red Lake" "Lake Ontario" "Lake Erie" "Salt Lake"
#[13] "Green Lake"

`%notin%` <- Negate(`%in%`)
us <- us2[us2$NAME_2 %notin% lakes.name, ]

然后您可以映射剩余的特征:

mainland2 <- ggplot(data = us) +
geom_sf(aes(fill = NAME_2), size = 0.4, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) + guides(fill = F)
mainland2

enter image description here

另一种方法(更简单但不太灵活)是通过从 ENGTYPE_2 中排除 Water body 值来映射县要素,如下所示:

us <- us2[(us2$ENGTYPE_2) != "Water body",]
mainland2 <- ggplot(data = us) +
geom_sf(aes(fill = NAME_2), size = 0.4, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) + guides(fill = F)
mainland2

enter image description here

关于从 R 中的美国县级 map 中删除五大湖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59113457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com