【发布时间】:2019-11-30 14:41:32
【问题描述】:
我有一篇较早的帖子here 使用来自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)
生成的图片如下: 可以看出,虽然五大湖地区不再用颜色编码,但州界仍然存在(红色箭头)。我想要一个如下图,其中各州被陆地边界隔开,并且没有州边界跨越湖区:
对于如何实现这一点的任何建议表示赞赏。
【问题讨论】:
-
也许使用另一张美国地图?
-
@Tjebo 您是否知道除了 GADM 之外,在哪里可以获得美国边界的所有三个级别的高质量 shapefile?我希望使用 shapefile,因为我更喜欢使用 sf 对象而不是老式的
geom_polygon(data = usa, aes(x=long, y = lat, group = group))方法。 -
查看此网站:r-spatial.org/r/2018/10/25/ggplot2-sf.html。和the package
rnaturalearth。显然,人们可以很容易地从世界数据中提取国家/大陆。您可以将其与 GADM 地图结合使用 - 无需仅使用来自一个来源的地图数据 :) - 例如,首先使用一个地图数据绘制州,然后使用另一个绘制县......希望这可能会有所帮助跨度>
标签: r ggplot2 maps sf maptools