Skip to content

Instantly share code, notes, and snippets.

@Guibrich
Created May 12, 2013 17:28
Show Gist options
  • Save Guibrich/5564319 to your computer and use it in GitHub Desktop.
Save Guibrich/5564319 to your computer and use it in GitHub Desktop.
lm_eqn = function(df) {
m = lm(pollution ~ pib, df);
l <- list(a = format(coef(m)[1], digits = 2),
b = format(abs(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3));
if (coef(m)[2] >= 0) {
eq <- substitute(italic(Pollution) == a + b %.% italic(PIB)*","~~italic(R)^2~"="~r2,l)
} else {
eq <- substitute(italic(Pollution) == a - b %.% italic(PIB)*","~~italic(R)^2~"="~r2,l)
}
as.character(as.expression(eq));
}
require(ggplot2)
p=ggplot(dd, aes(x=pib, y=pollution)) +geom_point(shape=5) ;
p1 = p + geom_text(aes(x =110, y = 110, label = lm_eqn(dd)), parse = TRUE)+
geom_smooth(method=lm)
print(p1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment