How can I remove the line aesthetic from a ggplot2 legend?


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

enter image description here

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?


Answers:


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

plot with no lines in legend