Creating a function
Another way is to build a layer via a function:
geom_lm <- function(formula = y ~ x, colour = alpha("steelblue", 0.5),
linewidth = 2, ...) {
geom_smooth(formula = formula, se = FALSE, method = "lm", colour = colour,
linewidth = linewidth, ...)
}
Use the layer in the plot:
ggplot(mpg, aes(displ, 1 / hwy)) +
geom_point() +
geom_lm(y ~ poly(x, 2), linewidth = 1, colour = "red")