简体   繁体   中英

How to change the glyph or icon for geom_abline in the legend?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM