How to add a trend line to a scatter plot in R
· 1 min read · Updated March 14, 2026 · beginner
r ggplot2 visualization regression statistics
Adding trend lines reveals relationships between variables in scatter plots.
With ggplot2
The geom_smooth() function adds trend lines with confidence intervals:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE)
Use method = "loess" for smoothed lines, or se = FALSE to hide confidence intervals:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE)
For polynomial trends, specify the formula:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ poly(x, 2))
With base R
Use abline() with lm() for linear regression:
plot(mtcars$wt, mtcars$mpg)
abline(lm(mtcars$mpg ~ mtcars$wt), col = "red")