install.packages("ggppubr") #install dplyr package
library(dplyr) #load dplyr
library(ggplot2) #load ggplot2
library(ggpubr)BIOL 204L Spring 2026
How to add a Linear Regression Line to your Scatter Plot in ggplot2
Installing and Loading Packages for Today: Step 1
You have already installed ggplot2 and dplyr in your Posit Cloud packages library. Today we will be using another package called ggpubr. We will need to install and load this package before we begin.
The ggpubr package provides some easy-to-use functions for creating and customizing ggplot2 - based publication ready figures. We will be using ggpubr to include linear regression equations and R-squared values to our figures.
Create a Simple Scatter Plot: Step 2
This code will not be the exact code you need to use but will be based off the iris dataset.
data("iris")
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() + #create scatter plot of sepal length v. sepal width
theme_classic() + #format figure to remove background and gridlines
xlab("Sepal Length (cm)") + #change x axis label
ylab("Sepal Width (cm)") #change y axis labelAdd a Linear Regression Model Line to your Scatter Plot: Step 3
We can clearly see from this data that the data is grouped by some factor, potentially species, but let’s see if there is a linear relationship betweeen sepal length and width for all species. To add a linear regression line, we need to use the geom_smooth() function in ggplot2 with the method as lm (linear model).
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() + #create scatter plot of sepal length v. sepal width
theme_classic() + #format figure to remove background and gridlines
xlab("Sepal Length (cm)") + #change x axis label
ylab("Sepal Width (cm)") + #change y axis label
geom_smooth(method = "lm", se = F, color="black") #add linear trendline`geom_smooth()` using formula = 'y ~ x'
Add the Linear Regression Equation and R-Squared Values to your Plot: Step 4
In order to add the linear regression equation and R-squared values we will use the stat_regline_equation() function in ggpubr. We can set where we want these to be in our figure with label.y and label.x (if wanted) arguments.
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() + #create scatter plot of sepal length v. sepal width
theme_classic() + #format figure to remove background and gridlines
xlab("Sepal Length (cm)") + #change x axis label
ylab("Sepal Width (cm)") + #change y axis label
geom_smooth(method = "lm", se = F, color="black") + #add linear trendline
#add linear regression line equation at y = 5
stat_regline_equation(label.y = 5, aes(label = ..eq.label..)) +
#add R-squared value at y = 4.5
stat_regline_equation(label.y = 4.5, aes(label = ..rr.label..))`geom_smooth()` using formula = 'y ~ x'
Now you should be able to use code similar to this to add it to your figures! Make sure your label.y values are appropriate for your figure. You may need to adjust them a couple of times.