Im following this example Add regression line legend to geom_abline
Is there a reason the legend line is slanting? Is it possible for the legend to have a straight red and blue line?
Try this. You can choose the glyph for the legend with argument key_glyph
. To get lines choose key_glyph = "path"
in both geom_abline
layers.
set.seed(1234)
X <- rnorm(20,sd=2.5)
y <- -1+0.5*X+rnorm(20, sd=0.4)
library(ggplot2)
ggplot() +
geom_point(mapping = aes(x = X, y = y)) +
geom_abline(aes(intercept = -0.9930872, slope = 0.4866284, colour = "line1"), lwd=1, key_glyph = "path") +
geom_abline(aes(intercept = -1, slope = 0.5, colour = "line2"), lwd=1, key_glyph = "path") +
scale_colour_manual(values=c("line1"="red","line2"="blue"))
Created on 2020-06-14 by the reprex package (v0.3.0)