我可以根据颜色选择填充物吗?
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")
那我来补一下:
- 与类别相同,如果
p
是 TRUE
- 否则为白色
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)
我有以下示例数据:
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")
那我来补一下:
- 与类别相同,如果
p
是TRUE
- 否则为白色
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)