Regression equation on plot

25524 views

Brandon Hurr

Jun 23, 2010, 7:53:47 AM6/23/10
to ggplot2
Hi all,

Just trying to do something simple. Plot a scatter plot and put a linear regression line, equation and r-squared on it. I've managed to get a good looking graph, but want an easy way to annotate the plot with this info. I may not even be doing it right. Here is my code...

lm.data1<-structure(list(Abbrcd = structure(c(23L, 21L, 3L, 8L, 22L, 15L,
9L, 16L, 28L, 17L, 24L, 4L, 25L, 10L, 18L, 5L, 26L, 19L, 27L,
32L, 6L, 11L, 12L, 20L, 13L, 31L, 14L, 7L, 29L, 30L, 2L), .Label = c("(Leny3-",
"Vibrant", "(Calorice5b", "Calorice",
"Funport", "LS792", "Knobless", "LS1PanF-",
"unly-", "Estonia", "L74",
"L878", "L974", "Mode", "1-11a-3-",
"asdf-2-1-1-1-", "jklas1-2an-",
"Fartly", "Iphone", "L73", "Uptown-2-",
"(6a)6-2-1-3-", "(asfeiflar",
"Caipira", "Comer", "Funtime", "Invention", "mama",
"PATRON", "Turbo", "Maritime", "LSummit"), class = "factor"),
dataset.avg = c(3.5, 3, 1.5, 2, 3, 2.5, 2, 2.5, 4, 2.5, 3.5,
1.5, 3.5, 2, 2.5, 1.5, 3.5, 2.5, 3.5, 5, 1.5, 2, 2, 2.5,
2, 4.5, 2, 1.5, 4, 4, 1), `WIBIday5.avg[, 3]` = c(4.125,
4.125, 4.75, 5.75, 4.25, 5.375, 5.14285714285714, 5.375,
5.125, 5.125, 5.375, 4.125, 3.5, 3.875, 3.75, 5.125, 6, 4.625,
5.125, 7.625, 2.875, 4, 4.75, 6.625, 5.125, 5.5, 4.875, 4.125,
4.25, 5.57142857142857, 4.125), `WIBIday10.avg[, 3]` = c(3.25,
2.875, 2.875, 4.375, 2.875, 4.5, 3.625, 4.375, 4.125, 3.5,
4.375, 3.25, 2.375, 3.125, 3.75, 3.875, 4.75, 3.25, 3.75,
5.875, 1.5, 2.625, 3.625, 5.5, 2.125, 4, 3, 2.125, 2.5, 4.625,
3.375)), .Names = c("Abbrcd", "dataset.avg", "WIBIday5.avg[, 3]",
"WIBIday10.avg[, 3]"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L), class = "data.frame")

ggplot(lm.data1, aes(x = lm.data1[[3]], y = lm.data1[[2]])) + geom_smooth(method = "lm") + geom_point()

Thanks,

Brandon

fernando

Jun 23, 2010, 4:48:57 PM6/23/10
to Brandon Hurr, ggplot2

Hi Brandon,

Probably there are better alternatives (I’m thinking in gridExtra, e.g. tableGrob), but hope the example bellow could help.

fernando

library(ggplot2)

df <- data.frame(x = c(1:100))

df\$y <- 2 + 3 * df\$x + rnorm(100, sd = 40)

m <- lm(y ~ x, data = df)

p <- ggplot(data = df, aes(x = x, y = y)) +

geom_smooth(method = "lm", formula = y ~ x) +

geom_point()

p

eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,

list(        a = format(coef(m)[1], digits = 4),

b = format(coef(m)[2], digits = 4),

r2 = format(summary(m)\$r.squared, digits = 3)))

dftext <- data.frame(x = 70, y = 50, eq = as.character(as.expression(eq)))

p + geom_text(aes(label = eq), data = dftext, parse = TRUE)

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

alain

Jun 24, 2010, 8:09:19 AM6/24/10
to ggplot2
Also interested by this helpful suggestion.
Is there any way to put on several lines this equation, something
similar to using \n as in label option of annotate ?

p + annotate(geom="text",x=10,y=200,label="y = -0.007x \n Another
text")

Alain

topcrown

Mar 8, 2014, 10:30:55 PM3/8/14
Hi- Can someone help me understand each line of code?  I am very new to ggplot2 and R for that matter trying to set it up for my data which is made up of 3 variables  One is Store, Sales1 and Sales2.  Trying to compare Sales1 vs. Sales 2.  Where would I import it into the code.  My data file is DF.  Thank you.
JOH

Roman Luštrik

Mar 9, 2014, 1:56:57 AM3/9/14
to topcrown, ggplot2, Brandon Hurr
Non ggplot2 related questions belong elsewhere. Have you adequately researched the subject by typing (for example) "linear regression r" into your search engine? Best way of understanding what code does is to run it and see what it's doing.

When everything else fails, I suggest you try stackoverflow.com.

HTH,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.