r - 使用 ggplot2 为不同的行重新添加图例
问题描述
我想添加一个图例,它会告诉使用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
例子:
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'))
推荐阅读
- kubernetes - 如果 .computed 被唤起,Kubernetes\Dask 上的 Xarray (rasterio) 无法找到路径
- awk - 哪些vimrc设置控制markdown中缩进awk代码的语法突出显示?
- c# - 从文件夹加载未定义的图像
- java - 从类路径加载的 servlet 上下文的热部署?
- java - 如何制作具有不同内容的重复通知
- oauth - 哪种 OIDC 解决方案?
- c++ - 如何使用 LLVM C++ API 从 LLVM IR 创建机器代码
- kubernetes - 手动启动 Kubernetes CronJob
- c# - ASP.NET MVC - Xml Export - 根据类属性格式化文件名
- mysql - MySQL查询多对多关系表