我想添加一个图例,它将告诉您哪种颜色表示使用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'))

有没有人能提出什么建议来解决这个问题?
谢谢
发布于 2020-05-11 21:42:44
将颜色参数放入aes中,并为其指定要在图例中显示的名称,然后在scale_color_manual中选择标题和颜色
示例:
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'))https://stackoverflow.com/questions/61730874
复制相似问题