View source: R/lm_eqn_function.R
lm_eqn_fxn | R Documentation |
This function places the equation used by geom_smooth(method='lm') onto a ggplot by creating a character string from the regression.
lm_eqn_fxn(df, y, x)
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) |
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
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.