Skip to content

Instantly share code, notes, and snippets.

@rohit-gupta
Created September 19, 2018 13:51
Show Gist options
  • Save rohit-gupta/d34db0108fc166b251ffbb80d0821404 to your computer and use it in GitHub Desktop.
Save rohit-gupta/d34db0108fc166b251ffbb80d0821404 to your computer and use it in GitHub Desktop.
Get equation text from a R lm (Linear Model) object for use as text in graph
# Get equation text from LM
# Format: y = a + b.x, R^2 = r^2
equation_text = function(x, round_dig = 2) {
lm_coef <- list(a = as.character(round(coef(x)[1], digits = round_dig)),
b = as.character(round(coef(x)[2], digits = round_dig)),
r2 = round(summary(x)$r.squared, digits = round_dig));
lm_eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2~"="~r2,lm_coef)
as.character(as.expression(lm_eq));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment