How to Add a Regression Line to a ggplot?
R-bloggers 2021-01-26
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Linear regression is arguably the most widely used statistical model out there. It’s simple and gives easily interpretable results. Since linear regression essentially fits a line to a set of points it can also be readily visualized. This post focuses on how to do that in R using the {ggplot2}
package.
Let’s start off by creating a scatter plot of weight (wt
) vs. horse power (hp
) of cars in the infamous mtcars
dataset.
library(ggplot2)data(mtcars)p <- ggplot(mtcars, aes(wt, hp)) + geom_point()p
There’s an obvious positive trend visible: the heavier a car is the higher its horse power tend to be.
Next, let’s add a smoother to make this trend even more apparent.
p + geom_smooth()
By default, geom_smooth()
adds a LOESS smoother to the data. That’s not what we’re after, though. To make geom_smooth()
draw a linear regression line we have to set the method
parameter to "lm"
which is short for “linear model”.
p + geom_smooth(method = "lm")
The gray shading around the line represents the 95% confidence interval. You can change the confidence interval level by changing the level
parameter. A value of 0.8
represents a 80% confidence interval.
p + geom_smooth(method = "lm", level = 0.8)
If you don’t want to show the confidence interval band at all, set the se
parameter to FALSE
.
p + geom_smooth(method = "lm", se = FALSE)
Sometimes a line is not a good fit to the data but a polynomial would be. So, how to add a polynomial regression line to a plot? To do so, we will still have to use geom_smooth()
with method = "lm"
but in addition specify the formula
parameter. By default, formula
is set to y ~ x
(read: y
as a function of x
). To draw a polynomial of degree n
you have to change the formula to y ~ poly(x, n)
. Here’s an example fitting a 2nd degree (quadratic) polynomial regression line.
ggplot(mtcars, aes(qsec, hp)) + geom_point() + geom_smooth(method = "lm", formula = y ~ poly(x, 2))
Now it’s your turn! Start a new R session, load some data, and create a ggplot with a linear regression line. Happy programming!
var vglnk = {key: '949efb41171ac6ec1bf7f206d57e90b8'}; (function(d, t) { var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;// s.defer = true;// s.src = '//cdn.viglink.com/api/vglnk.js'; s.src = 'https://www.r-bloggers.com/wp-content/uploads/2020/08/vglnk.js'; var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.