我正在尝试使用三个不同的彩色线为三个组创建一个绘图,但是只有两个组具有点标记。我可以使图正确显示,但图例在所有三个组中都显示相同的点标记。
我使用mpg数据集创建了一个可复制的示例
shouldComponentUpdate
“ 4”组在图例中不应有点标记,而“ r”组应显示三角形标记
预先感谢您的帮助
答案 0 :(得分:0)
在您的linetype
中将geom_line()
自变量与目标变量(在这种情况下为drv
)相加,即可得到预期的结果。
library(tidyverse)
ggplot(mpg) +
geom_line(mapping = aes(x = displ, y = cty, color = drv, linetype = drv), size = 1) +
geom_point(data = subset(mpg, drv != '4'), mapping = aes(x = displ, y = cty, color = drv, shape = drv), size = 3) +
scale_color_manual(name="Variable", labels = c("4", "f", "r"), values=c("4" = "#DA2128", "f" = "black", "r" = "blue")) +
scale_shape_manual(name="Variable", labels = c("f", "r"), values = c("f" = 16, "r" = 17), guide = FALSE)
答案 1 :(得分:0)
尝试一些字母
ggplot(mpg, aes(x = displ, y = cty, color = drv, shape = drv)) +
geom_line() +
geom_point(aes(alpha=drv), size = 3) +
scale_alpha_manual(values = c(0,1,1)) +
scale_shape_manual(values = c(1,16, 17))
或者简单地将形状4
设置为NA
ggplot(mpg, aes(x = displ, y = cty, color = drv, shape = drv)) +
geom_line() +
geom_point(size = 3) +
scale_shape_manual(values = c(NA, 16, 17)) +
scale_color_manual(values = c("#DA2128", "black", "blue"))
答案 2 :(得分:0)
您还可以为shape
禁用图例,并在shape
图例中手动设置color
的值:
ggplot(mpg) +
geom_line(mapping = aes(x = displ, y = cty, color = drv), size = 1) +
geom_point(data = subset(mpg, drv != '4'), mapping = aes(x = displ, y = cty, color = drv, shape = drv), size = 3) +
scale_color_manual(name="Variable", labels = c("4", "f", "r"), values=c("4" = "#DA2128", "f" = "black", "r" = "blue"), guide=guide_legend(override.aes = list(shape = c("4" = NA, "f" = 16, "r" = 17)))) +
scale_shape_manual(values = c("f" = 16, "r" = 17), guide=F)