lm_eqn_fxn: Place a lm equation onto a ggplot

View source: R/lm_eqn_function.R

lm_eqn_fxnR Documentation

Place a lm equation onto a ggplot

Description

This function places the equation used by geom_smooth(method='lm') onto a ggplot by creating a character string from the regression.

Usage

lm_eqn_fxn(df, y, x)

Arguments

df

A The dataframe used to create the regression

y

Regression dependant variable (y-axis of plot)

x

regression independant variable (x-axis of plot)

Details

Code for this function originated here: https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph and was adapted from here: https://groups.google.com/g/ggplot2/c/1TgH-kG5XMA

Examples

 x <- rnorm(100, 10, 3)
 y <- 2*x + 5 + rnorm(100, 0, 3)
 test.dat <- as.data.frame(cbind(x, y))
 p <- (ggplot2::ggplot(test.dat, aes(x,y)) 
		 + ggplot2::geom_point() 
		 + ggplot2::geom_smooth(method='lm',se=FALSE, color='yellow', size=1)
            + (ggplot2::geom_text(x=10,y=10,label=lm_eqn_fxn(test.dat, 'y','x'), color='black',parse=T, size = 3) )
		 )
 p

JenC36/JACtools documentation built on Oct. 20, 2024, 8:05 a.m.