我想添加一个图例来告诉使用哪种颜色代表哪条线ggplot2
。
我的代码如下:
require(lme4)
require(ggplot2)
m1 <- lmer(Reaction ~ 1+I(Days) + (1+ Days| Subject) , data = sleepstudy)
pred1new1=predict(m1,re.form=NA)
为了添加图例,我尝试过scale_colour_manual
,但没有成功。
p21 <- ggplot(data = sleepstudy, aes(x = Days, y = Reaction))
p21+ geom_point() + geom_smooth(col="blue")+ geom_line(aes(y=pred1new1,group = Subject) ,col="red", lwd = 0.5)+
scale_colour_manual(name = 'the colour',
values =c('blue'='blue','red'='red'), labels = c('smooth','pred'))
任何人都可以提出任何建议来解决这个问题吗?
谢谢
将颜色参数放入aes
并为其指定您想要在图例中显示的名称,然后在中选择标题和颜色scale_color_manual
Example:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(color='Smooth 1')) +
geom_smooth(aes(y = (hwy -1), color='Smooth 2')) +
scale_color_manual('Legend Title', values=c('Smooth 1'='blue', 'Smooth 2'='red'))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)