Table of Contents
Adding a regression equation to a plot in R allows for visual representation of the relationship between two variables and provides a mathematical model for future predictions. To do this, one can use the “abline” function and specify the slope and intercept of the regression line. This will add a straight line to the plot that best fits the data points. Additionally, one can use the “lm” function to calculate the regression equation and then use the “text” function to display the equation on the plot. These steps will provide a clear and concise way to showcase the regression equation on a plot in R.
Add a Regression Equation to a Plot in R
Often you may want to add a regression equation to a plot in R as follows:
Fortunately this is fairly easy to do using functions from the ggplot2 and ggpubr packages.
This tutorial provides a step-by-step example of how to use functions from these packages to add a regression equation to a plot in R.
Step 1: Create the Data
First, let’s create some fake data to work with:
#make this example reproducible set.seed(1) #create data frame df <- data.frame(x = c(1:100)) df$y <- 4*df$x + rnorm(100, sd=20) #view head of data frame head(df) x y 1 1 -8.529076 2 2 11.672866 3 3 -4.712572 4 4 47.905616 5 5 26.590155 6 6 7.590632
Step 2: Create the Plot with Regression Equation
Next, we’ll use the following syntax to create a scatterplot with a fitted regression line and equation:
#load necessary libraries library(ggplot2) library(ggpubr) #create plot with regression line and regression equation ggplot(data=df, aes(x=x, y=y)) + geom_smooth(method="lm") + geom_point() + stat_regline_equation(label.x=30, label.y=310)
This tells us that the fitted regression equation is:
y = 2.6 + 4*(x)
Note that label.x and label.y specify the (x,y) coordinates for the regression equation to be displayed.
Step 3: Add R-Squared to the Plot (Optional)
You can also add the R-squared value of the regression model if you’d like using the following syntax:
#load necessary libraries library(ggplot2) library(ggpubr) #create plot with regression line, regression equation, and R-squared ggplot(data=df, aes(x=x, y=y)) + geom_smooth(method="lm") + geom_point() + stat_regline_equation(label.x=30, label.y=310) + stat_cor(aes(label=..rr.label..), label.x=30, label.y=290)
The for this model turns out to be 0.98.
You can find more R tutorials on .