Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot dashed regression line with geom_smooth in ggplot2

Tags:

r

ggplot2

I have a simple plot in ggplot2 and want to add a dashed regression line. So far I have:

library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  theme_bw()

Which returns what I want, but with a solid line:

scatterplot with regression line

I want to make the line dashed. I think I should use scale_linetype_manual() but my attempts have been hacky.

A simple question, but I couldn't find a duplicate.

like image 651
Sam Firke Avatar asked Dec 11 '15 18:12

Sam Firke


1 Answers

As per the help page (see ?geom_smooth), linetype is one of the aesthetics geom_smooth understands.

So, you can adjust to use geom_smooth(method = "lm", se = FALSE, linetype="dashed")

library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, linetype = "dashed") +
  theme_bw()
like image 155
Jota Avatar answered Sep 22 '22 14:09

Jota