How can I plot a Linear Regression Line in ggplot2 (With Examples)?

To plot a Linear Regression Line in ggplot2, first you need to create a data frame containing the x and y variables. Next, you can use the geom_smooth() function in the ggplot2 package to fit a linear regression line to the data. Lastly, you can use the stat_smooth() function to control the appearance of the line, including the line type, color, and confidence interval. For examples of this process, please refer to the ggplot2 documentation.


You can use the R visualization library ggplot2 to plot a fitted linear regression model using the following basic syntax:

ggplot(data,aes(x, y)) +
  geom_point() +
  geom_smooth(method='lm')

The following example shows how to use this syntax in practice.

Example: Plot a Linear Regression Line in ggplot2

Suppose we fit a simple linear regression model to the following dataset:

#create dataset
data <- data.frame(y=c(6, 7, 7, 9, 12, 13, 13, 15, 16, 19, 22, 23, 23, 25, 26),
                   x=c(1, 2, 2, 3, 4, 4, 5, 6, 6, 8, 9, 9, 11, 12, 12))

#fit linear regression model to dataset and view model summary
model <- lm(y~x, data=data)
summary(model)

Call:
lm(formula = y ~ x, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.4444 -0.8013 -0.2426  0.5978  2.2363 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  4.20041    0.56730   7.404 5.16e-06 ***
x            1.84036    0.07857  23.423 5.13e-12 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.091 on 13 degrees of freedom
Multiple R-squared:  0.9769,	Adjusted R-squared:  0.9751 
F-statistic: 548.7 on 1 and 13 DF,  p-value: 5.13e-12

The following code shows how to visualize the fitted linear regression model:

library(ggplot2)

#create plot to visualize fitted linear regression model
ggplot(data,aes(x, y)) +
  geom_point() +
  geom_smooth(method='lm') 

Linear regression plot in ggplot2

By default, ggplot2 adds standard error lines to the chart. You can disable these by using the argument se=FALSE as follows:

library(ggplot2)

#create regression plot with no standard error lines
ggplot(data,aes(x, y)) +
  geom_point() +
  geom_smooth(method='lm', se=FALSE) 

Regression line ggplot2

Lastly, we can customize some aspects of the chart to make it more visually appealing:

library(ggplot2)

#create regression plot with customized style
ggplot(data,aes(x, y)) +
  geom_point() +
  geom_smooth(method='lm', se=FALSE, color='turquoise4') +
  theme_minimal() +
  labs(x='X Values', y='Y Values', title='Linear Regression Plot') +
  theme(plot.title = element_text(hjust=0.5, size=20, face='bold')) 

Custom regression line using ggplot2 in R

Refer to this post for a complete guide to the best ggplot2 themes.

An Introduction to Multiple Linear Regression in R
How to Plot a Confidence Interval in R

x