In my plot below, I have two separate sources of data
(dat
and dat2
) used in two different geom_smooth()
calls producing the black and the red regression lines (see pic below).
Is it possible to manually add another legend
that shows the black line is called "Between"
and red line is called "Within"
?
library(tidyverse)
dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/cw2.csv')
dat$groups <- factor(dat$groups)
dat2 <- dat %>% group_by(groups) %>% summarize(mean_x = mean(x),
mean_y = mean(y),
.groups = 'drop')
dat %>%
ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse(level = .6) +
geom_point(data = dat2,
mapping = aes(x = mean_x, y = mean_y,fill = factor(groups)),
size = 4, show.legend = F,shape=21) +
geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1),
method = "lm", se=F, color = 1, formula = 'y ~ x')+
geom_smooth(aes(group = 1),
method = "lm", se=F, color = 2, formula = 'y ~ x')+
scale_fill_manual(values=rep('black',3))