格式化多个 geom_sf 个图例

Format multiple geom_sf legends

我正在 ggplot 中处理多个 sf 几何图形,并希望以点、线和正方形(对于多边形)的形式显示图例。但是,geom_sf 图例结合了我下面显示的几何特征(即结合线和点):

library(ggplot2)
library(sf)

poly1 <- cbind(lon = c(5, 6, 7, 5), lat = c(52, 53, 51, 52))

poly <- st_sf(st_sfc(st_polygon(list(poly1))))
line <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5.5, 4.5), lat = c(53.5, 54.5))))))
point <- st_sf(st_sfc(st_point(cbind(lon = 5.5, lat = 52.7))))

ggplot() +
  geom_sf(data = poly, aes(fill = "A")) +
  geom_sf(data = point, aes(colour = "B"), show.legend = "point") +
  geom_sf(data = line, aes(colour = "C"), show.legend = "line") +
  scale_fill_manual(values = c("A" = "yellow")) +
  scale_colour_manual(values = c("B" = "pink", "C" = "purple")) +
  theme_minimal()

我想要三个独立的图例,一个黄色方块,一个粉红色的点,和一条紫色的线在同一张图片上,如下图所示。只有当我绘制单个几何而不是三个的组合时才会出现这种情况。

我查找了类似的主题,但其中 none 涉及点几何,即 https://github.com/tidyverse/ggplot2/issues/2460

有人可以提供任何见解吗?

GitHub 问题:https://github.com/tidyverse/ggplot2/issues/2763

我知道如何分离图例,它们现在只是合并,因为您要映射颜色两次。通过将形状映射到点并设置颜色,您可以解决这个问题:

ggplot() +
  geom_sf(data = poly, aes(fill = "A")) +
  geom_sf(data = point, aes(colour = "B"), show.legend = "point") +
  geom_sf(data = line, aes(shape = "C"), show.legend = "line", color = 'purple') +
  scale_fill_manual(name = NULL, values = c("A" = "yellow")) +
  scale_colour_manual(name = NULL, values = c("B" = "pink")) +
  scale_shape_discrete(
    name = NULL, 
    guide = guide_legend(override.aes = list(color = 'purple'))) +
  theme_minimal()

但是:点和线仍然出现在所有三个图例中。我认为他们不应该!也许你可以填写一个 github 问题。

受@Axeman this issue and this post评论的启发,使用guide_legend()中的override.aes参数解决了问题:

library(ggplot2)
library(sf)

poly1 <- cbind(lon = c(5, 6, 7, 5), lat = c(52, 53, 51, 52))

poly <- st_sf(st_sfc(st_polygon(list(poly1))))
line <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5.5, 4.5), lat = c(53.5, 54.5))))))
point <- st_sf(st_sfc(st_point(cbind(lon = 5.5, lat = 52.7))))

ggplot() +
  geom_sf(data = poly, aes(fill = "A")) +
  geom_sf(data = point, aes(colour = "B"), show.legend = "point") +
  geom_sf(data = line, aes(colour = "C"), show.legend = "line") +
  scale_fill_manual(values = c("A" = "yellow"), name = NULL,
                    guide = guide_legend(override.aes = list(linetype = "blank", shape = NA))) +
  scale_colour_manual(values = c("B" = "pink", "C" = "purple"), name = NULL,
                      guide = guide_legend(override.aes = list(linetype = c("blank", "solid"), 
                                                               shape = c(16, NA)))) +
  theme_minimal()