Reputation: 5239
I have a plot where I would like to use different colors for the points but plot the linear regression based on all of the points:
library(ggplot2)
set.seed(1)
df <- data.frame(x=rnorm(100),
y=rnorm(100),
group=factor(rep(1:2,each=50)))
ggplot(df,aes(x=x,y=y,color=group)) +
stat_smooth(aes(group=1), method="lm", fill=NA) +
geom_point() + theme_bw()
The problem is that when I use stat_smooth()
to add the regression line, it adds lines in the legend that I don't want. I can't override the color to remove the lines from the legend because I need the color for the points. How can I remove the lines from the legend but keep the points?
Upvotes: 3
Views: 2458
Reputation: 43334
All you need to do is add show.legend = FALSE
to stat_smooth
:
ggplot(df, aes(x = x, y = y, color = group, group = 1)) +
geom_smooth(method = "lm", se = FALSE, show.legend = FALSE) +
geom_point() +
theme_bw()
Upvotes: 9