3

Possible Duplicate:
ggplot2: Adding Regression Line Equation and R2 on graph

I'm graphing data in a scatter plot with

ggplot(work.rootsfnp.h1, aes(x=fnpltrfac, y=rootsscore, group=1)) + 
  geom_smooth(method=lm, se = F) + geom_point(shape=1)

Is there a "quick" way to add a basic legend that includes the formula of the line of best fit as well as the correlation coefficient?

Community
  • 1
  • 1
Jeff Erickson
  • 3,783
  • 8
  • 36
  • 43

2 Answers2

6

Not quick, but possible:

First, fit a model with lm

model <- lm(mpg ~ wt + factor(cyl), data=mtcars)

Then extract the coefficients and R^2, and construct expressions for each

x <- coef(model)
intercept <- signif(x[1], 3)
terms <- paste(signif(x[-1], 3), names(x[-1]), sep="*", collapse= " + ")
e1 <- paste(intercept, terms, collapse = " + ")
e2 <- paste("R^2 = ", round(summary(model)$r.squared, 3))

Finally, plot with ggplot and use annotate to place labels.

ggplot(mtcars, aes(x=wt, y=mpg)) + 
    geom_point() + 
    geom_smooth(method=lm) +
    annotate("text", label=e1, x=max(mtcars$wt), y=max(mtcars$mpg), 
             hjust=1, size=3, vjust=0) +
    annotate("text", label=e2, x=max(mtcars$wt), y=max(mtcars$mpg), 
             hjust=1, size=3, vjust=1)

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496
5

See Ramnath's answer to similar question that I asked sometime ago.

library(ggplot2)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)

# GET EQUATION AND R-SQUARED AS STRING
# SOURCE: http://goo.gl/K4yh

lm_eqn = function(df){
    m = lm(y ~ x, df);
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
         list(a = format(coef(m)[1], digits = 2), 
              b = format(coef(m)[2], digits = 2), 
             r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq));                 
}

p <- ggplot(data = df, aes(x = x, y = y)) +
            geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
            geom_point()

p <- p + geom_text(aes(x = 25, y = 300, label = lm_eqn(df)), parse = TRUE)
print(p)

Output

Community
  • 1
  • 1
MYaseen208
  • 22,666
  • 37
  • 165
  • 309