我可以根据颜色选择填充物吗?

Can I choose the fill based on the color?

我有以下示例数据:

d <- data.frame(x = 1:3, y = 1:3, category = letters[1:3], p = c(TRUE, TRUE, FALSE))

我想按类别绘制此颜色。

ggplot(d, aes(x, y, color = category) + geom_point(shape = "circle filled")

那我来补一下:

ggplot(d, aes(x, y, color = category, fill = ???) + geom_point(shape = "circle filled")

有人有什么建议吗?

我可以

您可以使用fill = after_scale(color) 将填充指定为与色标相同的值。使用 after_scale(ifelse(p, color, "white") 应该会为您提供可选的白色填充值。

唯一的小问题是我们在 aes 中使用的常用 NSE 在 after_scale 中不起作用,因此我们需要使用 d$p 而不是 p ] 对于 ifelse

的第一个参数
ggplot(d, aes(x, y, color = category)) +
  geom_point(aes(fill = after_scale(ifelse(d$p, color, "white"))),
             shape = "circle filled", size = 10)