Pat*_*ick 1 maps r ggplot2 r-maptools r-sf
我之前有一篇文章是关于使用GADM中的形状文件绘制美国地图,同时删除五大湖地区的颜色映射。根据@Majid 的建议解决了该问题。
现在,我进一步想要更厚的州边界和更薄的县边界。我首先绘制县级分区统计图,然后添加额外的未填充的州/国家级边界:
library(sf)
library(tidyverse)
library(RColorBrewer) #for some nice color palettes
# US map downloaded from https://gadm.org/download_country_v3.html
# National border
us0 <- st_read("<Path>\\gadm36_USA_0.shp")
# State border
us1 <- st_read("<Path>\\gadm36_USA_1.shp")
# County border
us2 <- st_read("<Path>\\gadm36_USA_2.shp")
# Remove the Great Lakes
# 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))
# Pick the Great Lakes
lakes.name <- lakes.name[c(4, 5, 7, 10, 11)]
`%notin%` <- Negate(`%in%`)
us2 <- us2[us2$NAME_2 %notin% lakes.name, ]
# National level
mainland0 <- ggplot(data = us0) +
geom_sf(fill = NA, size = 0.3, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000))
# State level
mainland1 <- ggplot(data = us1, size = 0.3, color = "black") +
geom_sf(fill = NA) +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000))
# County level
mainland2 <- ggplot(data = us2) +
geom_sf(aes(fill = NAME_2), size = 0.1, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000))+
guides(fill = F)
# Final plot across three levels
p <- mainland2 +
geom_sf(data = us1, fill = NA, size = 0.3, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) +
geom_sf(data = us0, fill = NA, size = 0.3, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) +
guides(fill = F)
Run Code Online (Sandbox Code Playgroud)
生成的图像如下:
可以看到,虽然五大湖地区不再用颜色编码,但州边界仍然存在(红色箭头)。我想要一个如下图,其中各州由陆地边界分隔,并且没有州边界跨越湖区:
任何有关如何实现这一目标的建议都将受到赞赏。
你甚至不需要us0
(国界)和us1
(州界)。这些已经存在于us2
. 您可以绘制所需的输出,如下所示:
us0 <- sf::st_union(us2)
us1 <- us2 %>%
group_by(NAME_1)%>%
summarise()
Run Code Online (Sandbox Code Playgroud)
和你绘制的图:
# Final plot across three levels
p <- mainland2 +
geom_sf(data = us1, fill = NA, size = 1.5, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) +
geom_sf(data = us0, fill = NA, size = 1.5, color = "black") +
coord_sf(crs = st_crs(2163),
xlim = c(-2500000, 2500000),
ylim = c(-2300000, 730000)) +
guides(fill = F)
p
Run Code Online (Sandbox Code Playgroud)