Sometimes we want to put regression equations or regression information on plots. We can extract the values we want from regression models like lm and then use annotate, geom_label, or geom_text to put values on the figures. Here are two methods using the mtcars data as an example. Note that we can use round to round the numbers to whatever significant figures we want. We also have to indicate the x and y values for where we want the text to go.
Load tidyverse and create the lm object.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
theme_set(theme_bw())
mymodel <- lm(hp ~ disp, data = mtcars)
summary(mymodel)
##
## Call:
## lm(formula = hp ~ disp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -48.623 -28.378 -6.558 13.588 157.562
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.7345 16.1289 2.836 0.00811 **
## disp 0.4375 0.0618 7.080 7.14e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 42.65 on 30 degrees of freedom
## Multiple R-squared: 0.6256, Adjusted R-squared: 0.6131
## F-statistic: 50.13 on 1 and 30 DF, p-value: 7.143e-08
mymodel$coefficients
## (Intercept) disp
## 45.7345322 0.4375526
round(mymodel$coefficients[2], 2)
## disp
## 0.44
annotateWe can grab the coefficients directly from the lm object and build the text we want with paste.
ggplot(mtcars, aes(x = disp, y = hp)) +
geom_point() +
geom_smooth(method = 'lm', color = "black") +
xlab("Displacement") +
ylab("Horsepower") +
annotate("text", x = 200, y = 10,
label = paste("Slope is ", round(mymodel$coefficients[2], 2),
"Intercept is ", round(mymodel$coefficients[1], 2)))
## `geom_smooth()` using formula 'y ~ x'
geom_label and geom_textHere, we can crearte a data.frame with the coefficients we want and give them the names we want and then call that data.frame with geom_label or geom_text.
mymodel_cofficients <- data.frame(slope = round(mymodel$coefficients[2], 2),
intercept = round(mymodel$coefficients[1], 2))
ggplot(mtcars, aes(x = disp, y = hp)) +
geom_point() +
geom_smooth(method = 'lm', color = "black") +
xlab("Displacement") +
ylab("Horsepower") +
geom_label(data = mymodel_cofficients, aes(x = 200, y = 30,
label = paste("slope =", slope, "intercept =", intercept))) +
geom_text(data = mymodel_cofficients, aes(x = 200, y = 10,
label = paste("slope =", slope, "intercept =", intercept)))
## `geom_smooth()` using formula 'y ~ x'