Regression line for the entire dataset together with regression lines based on groups in R ggplot2 ?

て烟熏妆下的殇ゞ 提交于 2019-12-18 04:19:08

问题


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) 

回答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()



来源:https://stackoverflow.com/questions/12927527/regression-line-for-the-entire-dataset-together-with-regression-lines-based-on-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!