In Chapter 2, Advanced Functions in Base Graphics, we will cover Ordinary Least Squares (OLS) regressions and plotting regression lines. However, if you are curious as to how to include a regression line, this is how it is done. Use the abline()
command (which draws lines) in conjunction with the lm()
command, which performs a regression.
The syntax for performing a regression on the two variables is lm(Y ~ X)
, where the tilde sign instructs R to perform the regression, with Y
as the dependent variable and X
as the independent variable. Now include the following syntax on the command line:
abline(lm(Y ~ X))
The following is your plot with a regression line: