ggplot2 中的条件图例符号

Conditional legend symbols in ggplot2

我需要根据数据中的某些条件更改 ggplot 图例中的符号。例如,这里我想要空心圆来表示 cyl 大于 4 的值:

library(ggplot2)
ggplot()+
  geom_point(data = mtcars[mtcars$gear >= 4,],
             aes(mpg,
                 disp,
                 size = gear),
             pch = 21) +
  geom_point(data = mtcars[mtcars$gear < 4,],
             aes(mpg,
                 disp,
                 size = gear)) +
  theme_minimal() +
  guides(size = guide_legend(override.aes = list(pch = c(19, 19, 21, 21, 21))))

我目前的方法是对图例的形状矢量进行硬编码:guides(size = guide_legend(override.aes = list(pch = c(19, 19, 21, 21, 21))))

但是我怎样才能通过自动化和概括来避免这种硬编码,这样我就可以更轻松地制作许多数据具有不同范围的图,我需要更改图例显示不同的条件?

我最终在形状参数上付出了努力:

ggplot()+
  geom_point(data = mtcars[mtcars$gear >= 4,],
             aes(mpg,
                 disp,
                 size = gear, shape=factor(1L+(cyl > 4)) ),
             pch = 21) +
  geom_point(data = mtcars[mtcars$gear < 4,],
             aes(mpg,
                 disp,
                 size = gear, shape=factor(1L+(cyl > 4) ))) +
  theme_minimal() +
  guides(size = guide_legend(override.aes = list(pch = c(19, 19, 21, 21, 21))))

我曾尝试 shape=1L+(cyl > 4) 但一直收到错误 "A continuous variable can not be mapped to shape" 这看起来很荒谬,因为没有办法将表达式解释为 "continuous variable"。可能需要对图例标题进行一些 guide 处理。

如果您知道要使用什么中断,那么您可以在 override.aes 中设置一个条件。对于连续刻度,我认为默认是 scales::cbreaks(DATA_RANGE, extended_breaks()).

library(ggplot2)
library(scales)

cutoff <- 4 # set the condition value here so we don't hard-code it in the plot code

ggplot(mtcars, aes(x = mpg, 
                   y = disp, 
                   size = gear, 
                   shape = gear >= cutoff)) +
  geom_point() +
  theme_minimal() +
  scale_shape_manual(values = c(19, 21),
                     name = "gear",
                     guide  = "none") +
  guides(size = guide_legend(override.aes = list(pch = ifelse(
    cbreaks(range(mtcars$gear), 
            extended_breaks())$breaks >= cutoff, 21, 19
  ))))