使用 GADM shapefile 为美国地图绘制更粗的州边界和更细的县边界
Drawing thicker state borders and thiner county borders for US map using GADM shapefile
我有一个较早的 post on drawing US maps using shape file from GADM,同时删除了五大湖地区的颜色映射。根据@Majid 的建议解决了这个问题。
现在,我还想要更厚的州边界和更薄的县边界。为此,我首先绘制了县级等值线图,然后添加了额外的非填充层 state/nation-level 边界:
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)
生成的图片如下:
可以看出,虽然五大湖地区不再用颜色编码,但州界仍然存在(红色箭头)。我想要一个如下图,其中各州被陆地边界分隔开,没有州界穿过湖区:
如有任何关于如何实现这一点的建议,我们将不胜感激。
您甚至不需要 us0
(国界)和 us1
(州界)。这些已存在于 us2
中。您可以按如下所示绘制所需的输出:
us0 <- sf::st_union(us2)
us1 <- us2 %>%
group_by(NAME_1)%>%
summarise()
以及您绘制的地块:
# 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
希望对您有所帮助。
我有一个较早的 post
现在,我还想要更厚的州边界和更薄的县边界。为此,我首先绘制了县级等值线图,然后添加了额外的非填充层 state/nation-level 边界:
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)
生成的图片如下:
如有任何关于如何实现这一点的建议,我们将不胜感激。
您甚至不需要 us0
(国界)和 us1
(州界)。这些已存在于 us2
中。您可以按如下所示绘制所需的输出:
us0 <- sf::st_union(us2)
us1 <- us2 %>%
group_by(NAME_1)%>%
summarise()
以及您绘制的地块:
# 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