gpt4 book ai didi

r - ggplot 图例-scale_colour_manual 不起作用

转载 作者:行者123 更新时间:2023-12-02 19:31:29 25 4
gpt4 key购买 nike

我正在尝试向我的图表添加图例,但没有显示任何内容。这是我的代码:

ggplot(main, aes(x = ceiling(session/2))) + 
geom_line(aes(y = C_overall), colour = "blue", stat = "summary", fun.y = "mean") +
geom_line(aes(y = I_overall), colour = "red", stat = "summary", fun.y = "mean") +
labs(title = 'Overall Accuracy', x = 'Session', y = 'Percent Accurate') +
facet_wrap(~bird)

这向我展示了我想要的东西,除了没有图例。我所看到的一切都说要像这样使用scale_colour_manual:

ggplot(main, aes(x = ceiling(session/2))) + 
geom_line(aes(y = C_overall), colour = "one", stat = "summary", fun.y = "mean") +
geom_line(aes(y = I_overall), colour = "two", stat = "summary", fun.y = "mean") +
labs(title = 'Overall Accuracy', x = 'Session', y = 'Percent Accurate') +
facet_wrap(~bird) +
scale_colour_manual(name = 'Congruency', values = c("one" = "blue", "two" = "red"))

这似乎对其他人都有效,但 R 只是告诉我“one”是无效的颜色名称。我已经为此工作了几个小时,但距离弄清楚这一点还差得很远。

以下是我的一些数据(如果有帮助的话):

bird   session  C_overall   I_overall
23W 1 42.5 42.5
23W 2 46.25 47.5
23W 3 51.25 57.5
23W 4 47.5 52.5
23W 5 47.5 52.5
23W 6 47.5 62.5
23W 7 52.5 52.5
23W 8 50 55
23W 9 51.25 52.5
23W 10 48.75 47.5
43R 1 47.5 42.5
43R 2 43.75 37.5
43R 3 58.75 40
43R 4 51.25 40
43R 5 51.25 52.5
43R 6 36.25 35
43R 7 53.75 40
43R 8 57.5 45
43R 9 61.25 52.5
43R 10 48.75 47.5
57Y 1 45 67.5
57Y 2 53.75 62.5
57Y 3 47.5 65
57Y 4 52.5 52.5
57Y 5 47.5 50
57Y 6 48.75 70
57Y 7 66.25 72.5
57Y 8 55 60
57Y 9 57.5 72.5
57Y 10 58.75 67.5
76B 1 51.25 50
76B 2 56.25 42.5
76B 3 60 60
76B 4 68.75 70
76B 5 73.75 75
76B 6 55 52.5
76B 7 68.75 62.5
76B 8 40 40
76B 9 57.5 55
76B 10 66.25 70

蓝线应为“一致”,红线应为“不一致”。

任何有关如何制作传奇的帮助将不胜感激!提前致谢!!

最佳答案

在绘图之前我会将数据转换为长格式:

library(reshape2)

main <- melt(main, c("bird", "session"))

ggplot(main, aes(x=ceiling(session/2), y=value, color=variable)) +
geom_line(stat="summary", fun.y="mean", size=1) +
labs(title="Overall Accuracy", x="Session", y="Percent Accurate") +
facet_wrap(~ bird) +
scale_color_discrete("Results", labels=c("Congruent", "Incongruent"))

enter image description here

这对你有用吗?

关于r - ggplot 图例-scale_colour_manual 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com