The regression line for the entire dataset along with the group-based regression lines in R ggplot2?

I am new to ggplot2 and have a problem displaying a regression line for the entire dataset along with regression lines for groups. So far I can build a regression line based on a group, but I have not been able to get a regression line for the entire data set in the same area. I want all regression lines to have a different line style so that they can be easily identified with black and white print. Any help would be greatly appreciated. here is my code:

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) 
+4
source share
1 answer

Try putting color, shape, line aesthetics, not in the original ggplot2 call

Then you can add a common line with a different color

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

enter image description here

+10
source

Source: https://habr.com/ru/post/1440205/


All Articles