How to Use stat_regline_equation() in R

Often you may want to display a fitted regression line equation on top of a scatterplot in ggplot2.

One of the easiest ways to do so is by using the stat_regline_equation() function from the ggpubr package in R, which can be used to perform this exact task.

The following example shows how to use the stat_regline_equation() function in practice.

Note: Before using the stat_regline_equation() function, you may need to first install the ggpubr package. You can use the following syntax to do so:

`install.packages('ggpubr')`

Once the ggpbur package has been installed, you can proceed to use the stat_regline_equation() function.

Example: How to Use the stat_regline_equation() Function in R

Suppose that we create the following data frame in R that contains one predictor variable (x) and one response variable (y):

```#create data frame
df <- 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))

#view data frame
df

y  x
1   6  1
2   7  2
3   7  2
4   9  3
5  12  4
6  13  4
7  13  5
8  15  6
9  16  6
10 19  8
11 22  9
12 23  9
13 23 11
14 25 12
15 26 12```

Suppose that we would like to fit a simple linear regression model to this dataset, using the x variable as the predictor variable and the y variable as the response variable.

We can use the following syntax with the lm() function to do so:

```#fit linear regression model to data frame
model <- lm(y~x, data=df)

#view model summary
summary(model)

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

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 output shows the fitted regression model.

From the coefficients in the output we can construct the fitted regression equation:

• y = 4.20041 + 1.84036(x)

Now suppose that we would like to display this fitted regression equation on a scatterplot in ggplot2 so that we can visualize the relationship between x and y along with a neat summary of the regression equation.

We can use the stat_regline_equation() function with the following syntax to do so:

```library(ggplot2)
library(ggpubr)

#create plot to visualize fitted linear regression model
ggplot(df, aes(x, y)) +
geom_point() +
stat_regline_equation()```

This produces the following plot:

Notice that the fitted regression equation is shown in the top left corner of the scatterplot.

Note that we could also use the geom_smooth() argument to display the actual fitted regression line:

```library(ggplot2)
library(ggpubr)

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

This produces the following plot:

Notice that the linear regression line along with standard error bars are now shown in the plot, along with the fitted regression line equation in the top left corner.

Note: We used the argument method=’lm’ within the geom_smooth() function to specify that we wanted to fit a linear model to the dataset. This ensures that the line shown in the plot matches the equation shown by the stat_regline_equation() function.