将 ggplot2 网格线移动到顶层但保留面板背景

Move ggplot2 gridlines to top layer but keep panel background

sf 包与 ggplot2 一起使用,网格线用于绘制经纬网。通过将 panel.ontop 设置为 TRUE,可以在地图顶部显示经纬网:

library(sf)
library(ggplot2)

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

ggplot() +
    geom_sf(data = nc, fill='white') +
    coord_sf(expand = T) +
    theme(panel.border = element_rect(color = 'black', fill = NA),
          panel.background = element_blank(),
          panel.ontop = T,
          plot.background = element_rect(fill = "#ddefff"),
          axis.ticks = element_blank(),
          axis.text = element_blank()) +
    ggtitle("NC")

然而,我想要的是能够设置绘图区域的背景颜色,这通常使用 panel.background 来完成。这显然在这里行不通,因为它会覆盖地图。

想要的结果:

我最初的想法是,我可以先添加一个 geom_rect 面板区域的确切大小,但我不确定如何获得这些尺寸。如何将网格线保持在顶部,但仍具有独特的面板背景颜色?

编辑

加上下面@sebdalgarno 的好答案,我发现我们可以使用 expand_range 解决扩展问题(正如 ggplot2 在内部所做的那样):

bb <- st_bbox(nc)
bb[c(1,3,2,4)] <- c(scales::expand_range(c(bb$xmin, bb$xmax), mul = 0.05),
                    scales::expand_range(c(bb$ymin, bb$ymax), mul = 0.05))
bb <- bb %>% st_as_sfc() %>% st_sf()

这现在提供与设置 coord_sf(expand = T) 相同的扩展系数。

此解决方案适用于 expand = F:

根据您的 geom_rect 建议将 bbox 变成 sf 多边形

bb <- st_bbox(nc) %>% st_as_sfc() %>% st_sf()

并用 geom_sf

绘图
ggplot() +
  geom_sf(data = bb, fill = 'grey') +
  geom_sf(data = nc, fill='white') +
  coord_sf(expand = F) +
  theme(panel.border = element_rect(color = 'black', fill = NA),
        panel.background = element_blank(),
        panel.ontop = T,
        plot.background = element_rect(fill = "#ddefff"),
        axis.ticks = element_blank(),
        axis.text = element_blank()) +
  ggtitle("NC")

如果你能弄清楚如何手动将 bbox 多边形扩展相同的量,这可以使用 expand = T