这是示例数据帧:
dt <- data.frame(x = 1:6, y2017 = 5:10, y2018 = 12:17, y2019 = 15:20)
# x y2017 y2018 y2019
# 1 5 12 15
# 2 6 13 16
# 3 7 14 17
# 4 8 15 18
# 5 9 16 19
# 6 10 17 20
这是我最初所做的:
ggplot(dt, aes(x = x)) +
geom_point(aes(y = y2017, color = "2017", fill = "2017", shape = "2017"), size = 2) +
geom_line(aes(y = y2017, color = "2017")) +
geom_point(aes(y = y2018, color = "2018", fill = "2018", shape = "2018"), size = 2) +
geom_point(aes(y = y2019, color = "2019", fill = "2019", shape = "2019"), size = 2) +
ylab("Y") +
scale_colour_manual(name = "",
breaks = c("2017", "2018","2019"),
values = c("#D16103", "#52854C", "#293352")) +
scale_shape_manual(name = "",
breaks = c("2017", "2018","2019"),
values = c(3, 4, 21)) +
scale_fill_manual(name = "",
breaks = c("2017", "2018","2019"),
values = c("#D16103", "#52854C", "#293352")) +
theme_bw()
对于Legend2018
和2019
,有一条直线贯穿这一点,这不是我想要的。于是,我试着继续color
在geom_line
中2017
出的aes()
。
ggplot(dt, aes(x = x)) +
geom_point(aes(y = y2017, color = "2017", fill = "2017", shape = "2017"), size = 2) +
# Here is what I change:
geom_line(aes(y = y2017), color = "#D16103") +
geom_point(aes(y = y2018, color = "2018", fill = "2018", shape = "2018"), size = 2) +
geom_point(aes(y = y2019, color = "2019", fill = "2019", shape = "2019"), size = 2) +
ylab("Y") +
scale_colour_manual(name = "",
breaks = c("2017", "2018","2019"),
values = c("#D16103", "#52854C", "#293352")) +
scale_shape_manual(name = "",
breaks = c("2017", "2018","2019"),
values = c(3, 4, 21)) +
scale_fill_manual(name = "",
breaks = c("2017", "2018","2019"),
values = c("#D16103", "#52854C", "#293352")) +
theme_bw()
在的图例中2017
,与之交叉的线不再存在,而2018
和2019
是正确的。
我不知道是否有一种方法允许的传奇组合2017
在第一张图和2018
和2019
第二图。
(我知道我可以pivot_longer()
用来变换数据集并使其更容易绘制图例,但是对于我的主要数据集来说,这变得更加困难。所以我坚持这种方式)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句