ggplot图例 - scale_colour_manual不起作用 [英] ggplot legend - scale_colour_manual not working

查看:3216
本文介绍了ggplot图例 - scale_colour_manual不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ pre $ g $ p $ ggplot(main,aes(x = ceiling(session / 2)))+
geom_line(aes(y = C_overall),color =blue,stat =summary,fun.y =mean)+
geom_line(aes(y = I_overall),color =red ,统计=总结,fun.y =平均)+
实验室(title ='整体准确度',x ='会话',y ='准确百分比')+
facet_wrap(〜鸟)

这表明我想要什么,除了没有图例。我见过的所有东西都说要像这样使用scale_colour_manual:

  ggplot(main,aes(x = ceiling(session / 2) ))+ 
geom_line(aes(y = C_overall),color =one,stat =summary,fun.y =mean)+
geom_line(aes(y = I_overall), color =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'是无效的颜色名称。
我已经在这上面工作了好几个小时了,而且我无法接近解决这个问题。



如果有帮助,以下是我的一些数据: p>

 鸟会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))
$ b $ ggplot(main,aes(x = ceiling(session / 2),y = value,color = variable))+
geom_line(stat =summary,fun.y =平均值,size = 1)+
labs(title =整体准确度,x =Session,y =Percent Accurate)+
facet_wrap(〜bird)+
scale_color_discrete (Results,labels = c(Congruent,Incongruent))



这会对你有用吗?


I am trying to add a legend to my graphs, but nothing ever shows up. This is the code I have:

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)

This shows me what I want, except with no legend. Everything I've seen says to use scale_colour_manual like this:

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"))

This seems to work for everyone else, but R just tells me that 'one' is an invalid color name. I've worked on this for hours and I'm nowhere closer to figuring this out.

Here is some of my data if it's helpful:

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

The blue line should be "Congruent" and the red line should be "Incongruent".

Any help about how to make a legend would be greatly appreciated! Thanks in advance!!

解决方案

I would convert the data into long format before plotting:

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"))

Would that work for you?

这篇关于ggplot图例 - scale_colour_manual不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆