A Quick and Easy Way to Plot a Linear Regression Line in R

How to plot linear regression lines in R.

This tutorial provides a quick and easy way to plot a linear regression line in R using both base R and the ggplot2 package.

How to Plot a Linear Regression Line in Base R

The following code illustrates how to create a data frame and create a scatter plot of the data:

#create data frame
data <- data.frame(x = c(1, 3, 5, 6, 5, 8, 9, 12, 13, 13),
                   y = c(23, 25, 26, 25, 28, 36, 32, 35, 40, 41))

#view data
data

#    x  y
#1   1 23
#2   3 25
#3   5 26
#4   6 25
#5   5 28
#6   8 36
#7   9 32
#8  12 35
#9  13 40
#10 13 41

#attach data
attach(data)

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

Scatterplot in R

Next, we can fit a simple linear regression model to the data, then plot the line of best fit produced by this model using the abline() function:

#fit linear regression model to data
model <- lm(y ~ x)

#add linear regression line of best fit to the scatterplot
#abline(model)

Plot line of best fit using abline() in R

The reason this works is because the abline() function takes two arguments that specify the intercept and the slope of a straight line and the lm() function returns a value for both an intercept and a slope. For example, our lm() model in this example returns the values 20.142 and 1.461:

#fit linear regression model to data
model <- lm(y ~ x)

#view model parameters
model

#Call:
#lm(formula = y ~ x)
#
#Coefficients:
#(Intercept)            x  
#     20.142        1.461 

Thus, we could create the exact same regression line using the following code:

abline(20.142, 1.461)

Plot line of best fit using abline() in R

Lastly, we can modify the aesthetics of the graph a bit to make it look better:

#create scatterplot
plot(x, y, main = 'Simple linear regression line', xlab = 'Predictor',
     ylab = 'Response', pch = 16, col = 'steelblue')

#add line of best fit
abline(20.142, 1.461)

Abline example in base R

How to Plot a Linear Regression Line in R Using ggplot2

The following code illustrates how to create the same scatterplot with the line of best fit using the ggplot2 package:

#load ggplot2
library(ggplot2)

#create data frame
data <- data.frame(x = c(1, 3, 5, 6, 5, 8, 9, 12, 13, 13),
                   y = c(23, 25, 26, 25, 28, 36, 32, 35, 40, 41))

#create scatterplot with line of best fit
ggplot(data, aes(x = x, y = y)) + 
  geom_point() +
  stat_smooth(method = 'lm', col = 'steelblue')

Regression line with scatterplot in R

By default, the stat_smooth() function provides standard error lines on the plot. We can choose to hide these lines by using the argument se = FALSE:

ggplot(data, aes(x = x, y = y)) + 
  geom_point() +
  stat_smooth(method = 'lm', col = 'steelblue', se = FALSE)

Scatterplot in R with regression line

Lastly, we can add a title, axis labels, and change the theme if we’d like:

ggplot(data, aes(x = x, y = y)) + 
  geom_point() +
  stat_smooth(method = 'lm', col = 'steelblue', se = FALSE) +
  labs(title = 'Simple linear regression line', x = 'Predictor', y = 'Response') + 
  theme_classic()

Simple linear regression line in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *