本文介绍了从R中的美国县级地图中删除五大湖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R在县级绘制美国地图.我从 GADM 下载了美国的shapefile.县级形状文件是"gadm36_USA_2.shp".然后,我使用下面的代码绘制地图:

I am using R to draw US map at county level. I downloaded the shapefile for US from GADM. The county-level shape file is "gadm36_USA_2.shp". I then used the code below to draw 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)

绘制了大湖区(用红色箭头显示),而不是空白:

The Great Lakes region (shown by red arrows) is plotted rather than left blank:

我想要的是下图,大湖区留为空白:

What I want is a figure like below, where the Great Lakes region is left blank:

我如何从"gadm36_USA_2.shp"中识别出与大湖区相对应的行,以便删除它们?

How could I identify from the "gadm36_USA_2.shp" which rows correspond to the Great Lakes region so that I may delete them?

我知道,除GADM之外,还有其他获取shapefile的方法.我相信GADM是在全球范围内提供驻地的绝佳来源.我希望借此机会更好地了解从GADM下载的数据.

I understand there may be other ways to obtain shapefile than GADM. I believe GADM is an excellent source that provides bourndaries worldwide. I wish to take this opportunity to better acquaint myself with data downloaded from GADM.

当然,欢迎使用其他方法来获取美国县级边界数据.我注意到USAboundaries软件包还提供了国家,州和县级别的公文,但是我在安装关联的USAboundariesData软件包时遇到了困难.除了GADM的shapefile之外,任何以美国方式吸引美国各州的想法都欢迎.谢谢.

Of course, other methods to obtain US county-level boundary data are welcome. I noted USAboundaries package also provide country, state, and county level coundaries, but I am having difficulties installing associated USAboundariesData package. Any idea to draw US counties in ways other than shapefile from GADM is welcome. Thanks.

推荐答案

一种方法是删除现有记录中的所有带有Lake标签的功能(当前为13个功能).首先,您需要在属性表中找到湖泊名称,如下所示:

One way is to remove every feature that is tagged with Lake in the existing records (currently 13 features). First, you need to find the lakes name in the attribute table as below:

# 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, ]

然后您可以映射其余功能:

Then you can map the remaining features:

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

另一种方法(容易得多,但灵活性较差)是通过从ENGTYPE_2中排除Water body值来映射县要素,如下所示:

Another way (much easier but less flexible) is to map county features by excluding Water body values from ENGTYPE_2 as below:

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

这篇关于从R中的美国县级地图中删除五大湖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 23:20