1

I made this graph in R:

library(ggplot2)
set.seed(1)
x <- rnorm(100)
eps <- rnorm(100, sd=0.25)
y <- -1+0.5*x+eps
arq <- data.frame(X=x, Y=y)
disp <- ggplot(arq)+geom_point(aes(X,Y), col="gold")+theme_bw()+
  geom_smooth(aes(X,Y,color="Regression"), method=lm, se=FALSE, size=0.5, col="red", show.legend=TRUE)+
  geom_abline(aes(slope=0.5,intercept=-1,color="True"), col="blue", size=0.5, show.legend=TRUE)+
  scale_color_manual(values=c("red","blue"))

enter image description here

I thought this configuration would show a legend of true relation line and regression line, but not happened. What I am missing?

How can I extend the regression line?

kurtzdoni
  • 125
  • 3
  • you should expect a legend when you *do not* set `colour` and/or `fill` manually: see https://stackoverflow.com/questions/18394391/r-custom-legend-for-multiple-layer-ggplot – Pasqui May 18 '18 at 16:53

1 Answers1

1

This might be what you want

disp <- ggplot(arq)+geom_point(aes(X,Y), col="gold")+
 geom_smooth(aes(X,Y,color="Regression"), method=lm, se=FALSE, size=0.5)+
 geom_abline(aes(slope=0.5,intercept=-1,color="True"), size=0.5, show.legend = FALSE)+
 scale_color_manual(values=c("red","blue"))

Setting the col="red" and col="blue" options is causing the legend not to show. Having the color arguments inside aes() will produce the legend.

Also, using the show.legend = FALSE argument in geom_abline() will keep the legend from having the slashes in it.

StephenK
  • 685
  • 5
  • 16