整个数据集的回归线和基于R ggplot2中的组的回归线? [英] Regression line for the entire dataset together with regression lines based on groups in R ggplot2 ?

查看:383
本文介绍了整个数据集的回归线和基于R ggplot2中的组的回归线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ggplot2的新手,有问题显示整个数据集的回归线以及组的回归线。到目前为止,我可以绘制基于组的回归曲线,但我无法获得同一图上整个数据集的回归曲线。我希望所有具有不同线条样式的回归线都能在黑白打印中轻松识别。任何帮助将不胜感激。这里是我的代码到目前为止:

pre $ g $ pggplot(alldata,aes(y = y,x = x,color = group,shape = group))+
geom_point(size = 3,alpha = .8)+ geom_smooth(method =lm,fill = NA,size = 1)
pre>

解决方案

尝试将原始调用中的颜色,形状,线型美学设置为 ggplot2



然后,您可以用不同的颜色添加整条线

  set.seed(1)
library(plyr)
alldata< - ddply(data.frame(group = letters [1:5],x = rnorm(50)), 'group',
mutate,y = runif(1,-1,1)* x + rnorm(10))



ggplot(alldata,aes (y = y,x = x))+
geom_point(aes(color = group,shape = group),size = 3,alpha = .8)+
geom_smooth(method =lm, se = F,size = 1,aes(linetype = group,group = group))+
geom_smooth(method ='lm',size = 1,color ='black',se = F)+ theme_bw


I am new to ggplot2 and have problem displaying the regression line for the entire data-set together with the regression lines for groups. So far i can plot regression line based on the group but I have no success in getting the regression line for the entire data-set on the same plot. I want all the regression lines with different line style so that they can be easily identified in black and white print. Any help would be highly appreciated. here is my code so far:

ggplot(alldata,aes(y = y, x = x, colour= group, shape= group )) +
geom_point(size = 3, alpha = .8) + geom_smooth(method="lm", fill=NA , size = 1) 

解决方案

Try placing the colour, shape, linetype aesthetics not in the original call to ggplot2

You can then add the overall line with a different colour

set.seed(1)
library(plyr)
alldata <- ddply(data.frame(group = letters[1:5], x = rnorm(50)), 'group', 
                 mutate, y=runif(1,-1,1) * x +rnorm(10))



ggplot(alldata,aes(y = y, x = x)) +
     geom_point(aes(colour= group, shape= group), size = 3, alpha = .8) + 
     geom_smooth(method="lm", se= F, size = 1, aes(linetype = group, group = group)) +
     geom_smooth(method = 'lm',size = 1, colour = 'black', se = F) + theme_bw()

这篇关于整个数据集的回归线和基于R ggplot2中的组的回归线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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