Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change line border color in geom_smooth

How does one change the color of the line border in geom_smooth()?

library(ggplot2)
mtcars$cyl <- as.factor(mtcars$cyl)

ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
  geom_point() + 
  geom_smooth(method=lm)

enter image description here

It should look something like this:

enter image description here

Thank you for your time!

like image 614
mvanaman Avatar asked Oct 26 '25 18:10

mvanaman


1 Answers

One way is to plot wider, black lines first. Note that you need to add group = cyl for this to work properly.

ggplot(mtcars, aes(x=wt, y=mpg, group = cyl, color = cyl)) +
  geom_point() + 
  geom_smooth(method = lm, size = 2.5, color = "black", se = FALSE) + 
  geom_smooth(method = lm)

enter image description here

like image 161
slamballais Avatar answered Oct 28 '25 09:10

slamballais