按纬度或经度将地图拆分为两个单独的地图

Split a map into two separate maps by latitude or longitude

有没有办法将 ggplot2 地图分割成两个单独的地图?我有一张大地图,上面的 ID 标签难以辨认。我想将地图垂直拆分为两个不同的地图,最好具有重叠区域,以便每个多边形至少在一张地图中完整显示。

这是一个可重现的例子。我想将地图拆分为北纬 35 度的北部地图,然后拆分为北纬 35.5 度的南部地图(两者重叠 35 度和 35.5 度)。 (虽然我意识到这个例子以另一种方式分割可能更有意义,但我的实际地图在垂直方向上很长。)

library(sf)
library(ggplot2)

sf_nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
plot <- ggplot2::ggplot(sf_nc) +
  geom_sf(aes(color = NAME)) +
  geom_sf_text(aes(label = NAME)) 

也许这就是您要找的。

  1. 之后,我首先使用st_crop按纬度拆分sf df并提取南北地区的FIPS代码。
  2. 然后使用 FIPS 代码将 sf 数据帧一分为二,以确保分界线上的区域在两张地图中全部显示。
  3. 最后,我添加了一个 ID 并将两个 dfs 重新绑定在一起,以便使用 facet_wrap
  4. 轻松绘图
library(sf)
library(ggplot2)
library(dplyr)

sf_nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

# Get FIPS/regiona codes for north regions
south <- st_crop(sf_nc, xmin=-180, xmax=180, ymin=-90, ymax=35.5) %>% 
  pull(FIPS)

north <- st_crop(sf_nc, xmin=-180, xmax=180, ymin=35.5, ymax=90) %>% 
  pull(FIPS)

# Make sf df for north and south
sf_nc_1 <- filter(sf_nc, FIPS %in% south) %>% 
  mutate(id = "South")
sf_nc_2 <- filter(sf_nc, FIPS %in% north) %>% 
  mutate(id = "North")

# Bind together for using facet_wrap
sf_nc_split <- rbind(sf_nc_1, sf_nc_2)

ggplot2::ggplot(sf_nc_split) +
  geom_sf(aes(color = NAME)) +
  geom_sf_text(aes(label = NAME), size = 2) +
  guides(color = FALSE) +
  facet_wrap(~id, ncol = 1) +
  theme_void()