Home » How to Plot Line of Best Fit in R (With Examples)

How to Plot Line of Best Fit in R (With Examples)

by Tutor Aspire

You can use one of the following methods to plot a line of best fit in R:

Method 1: Plot Line of Best Fit in Base R

#create scatter plot of x vs. y
plot(x, y)

#add line of best fit to scatter plot
abline(lm(y ~ x))

Method 2: Plot Line of Best Fit in ggplot2

library(ggplot2)

#create scatter plot with line of best fit
ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    geom_smooth(method=lm, se=FALSE)

The following examples show how to use each method in practice.

Example 1: Plot Line of Best Fit in Base R

The following code shows how to plot a line of best fit for a simple linear regression model using base R:

#define data
x 

#create scatter plot of x vs. y
plot(x, y)

#add line of best fit to scatter plot
abline(lm(y ~ x))

Feel free to modify the style of the points and the line as well:

#define data
x 

#create scatter plot of x vs. y
plot(x, y, pch=16, col='red', cex=1.2)

#add line of best fit to scatter plot
abline(lm(y ~ x), col='blue' , lty='dashed')

We can also use the following code to quickly calculate the line of best fit:

#find regression model coefficients
summary(lm(y ~ x))$coefficients

              Estimate Std. Error   t value     Pr(>|t|)
(Intercept) -0.8928571  1.0047365 -0.888648 4.084029e-01
x            2.3095238  0.1989675 11.607544 2.461303e-05

The line of best fit turns out to be: y = -0.89 + 2.32x.

Example 2: Plot Line of Best Fit in ggplot2

The following code shows how to plot a line of best fit for a simple linear regression model using the ggplot2 data visualization package:

library(ggplot2)

#define data
df frame(x=c(1, 2, 3, 4, 5, 6, 7, 8),
                 y=c(2, 5, 6, 7, 9, 12, 16, 19))

#create scatter plot with line of best fit
ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    geom_smooth(method=lm, se=FALSE)

Feel free to modify the aesthetics of the plot as well:

library(ggplot2)

#define data
df frame(x=c(1, 2, 3, 4, 5, 6, 7, 8),
                 y=c(2, 5, 6, 7, 9, 12, 16, 19))

#create scatter plot with line of best fit
ggplot(df, aes(x=x, y=y)) +
    geom_point(col='red', size=2) +
    geom_smooth(method=lm, se=FALSE, col='purple', linetype='dashed') +
    theme_bw()

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Perform Simple Linear Regression in R
How to Perform Multiple Linear Regression in R
How to Interpret Regression Output in R

You may also like