Printing a label with an exponent (r squared) using geom_text

9,287 views
Skip to first unread message

Jennifer Krauel

unread,
Apr 9, 2013, 9:41:34 AM4/9/13
to ggp...@googlegroups.com
I am trying to do what seems like a pretty straightforward thing: print the r squared and p values on my graph.

I have looked at a lot of code samples to get the "squared" to appear as an exponent. It seems to me the code below should work (it does in a regular plot). It does not work for me. Hopefully it will allow you to reproduce the error I get from geom_text:

Don't know how to automatically pick scale for object of type expression. Defaulting to continuous
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class '"expression"' into a data.frame


I can figure out how to get the actual r squared value and p value out of the regression summary, but hey I'm willing to hard-code them like this if necessary.  Can someone give me the code that works, or if you can't reproduce the error with this code, help me understand why it doesn't work on my system?

Thanks,

Jennifer

-------------------------------------------------------
library(ggplot2) 

test.data <- structure(list(X1 = c(13, 2, 119, 7, 12, 11, 347, 41, 10, 4, 8, 112, 68, 102, 98, 165, 19, 2, 22, 25, 51), X2 = c(434.022499, 570, 2556, 1284, 457, 5114, 58215.5065, 581, 438, 233, 204, 497865.973, 3171.06517, 34728, 737386, 737386, 8433.52505, 355, 1715, 1715, 3851)), .Names = c("tabr", "edge"), row.names = c(NA, -21L), class = "data.frame")

ggplot (data = test.data, aes(x = log(edge), y = log(tabr))) +
          geom_point(shape=1) + 
          geom_smooth(method=lm, se=FALSE) + 
          geom_text(aes(x = 7.5, y = 5.5, 
                         label = expression(paste("r"^2, " = 0.585; p < 0.001"))), parse = TRUE)

Ulises M. Alvarez

unread,
Apr 9, 2013, 9:55:31 AM4/9/13
to Jennifer Krauel, ggp...@googlegroups.com
On 04/09/2013 08:41 AM, Jennifer Krauel wrote:
> I am trying to do what seems like a pretty straightforward thing: print
> the r squared and p values on my graph.


Take a look at:
http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph


--
Ulises M. Alvarez
http://sophie.unam.mx/

Jennifer Krauel

unread,
Apr 9, 2013, 11:53:50 AM4/9/13
to Ulises M. Alvarez, ggp...@googlegroups.com
Thanks Ulises, I did see that code and got it to work.  It does not do exactly what I want, however; I'm not interested in printing the equation, just the r squared but also the p value.  When I tried to modify it, I couldn't get it to work either.  What I'd really like is a simple solution that just works with my super simple code fragment.

Ben Bond-Lamberty

unread,
Apr 9, 2013, 12:46:58 PM4/9/13
to Jennifer Krauel, ggplot2
Hi Jennifer, try this. -Ben


m <- lm( log(tabr)~log(edge), data=test.data )
ms <- summary(m)

l <- list(r2 = format(ms$r.squared, digits = 3),
pval = format(pf(ms$fstatistic[1],ms$fstatistic[2],ms$fstatistic[3],lower.tail=FALSE), digits=3)
)

eq <- substitute(italic(r)^2 == r2*","~~italic(p) == pval,l)

eqstr <- as.character(as.expression(eq))

ggplot (data = test.data, aes(x = log(edge), y = log(tabr))) + 
          geom_point(shape=1) + 
          geom_smooth(method=lm, se=FALSE) + 
          annotate(geom="text",x = 7.5, y = 5.5, label = eqstr, parse = TRUE)



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jennifer Krauel

unread,
Apr 9, 2013, 1:35:46 PM4/9/13
to Ben Bond-Lamberty, ggplot2
Thanks, Ben, works like a charm.

Dennis Murphy

unread,
Apr 10, 2013, 12:12:47 AM4/10/13
to Jennifer Krauel, ggplot2
Sorry to be late to the party, but here are a couple of other ways to
do this making a couple of simple modifications to your original
ggplot() call:

ggplot(data = test.data, aes(x = log(edge), y = log(tabr))) +
geom_point(shape=1) +
geom_smooth(method=lm, se=FALSE) +
geom_text(aes(x = 7.5, y = 5.5,
label = "r^2 == 0.585~~~p < 0.001"), parse = TRUE)

ggplot(data = test.data, aes(x = log(edge), y = log(tabr))) +
geom_point(shape=1) +
geom_smooth(method=lm, se=FALSE) +
geom_text(aes(x = 7.5, y = 5.5,
label = "r^2 == 0.585"), parse = TRUE) +
geom_text(aes(x = 7.5, y = 5.2,
label = "p < 0.001"))

To use plotmath code in geom_text(), pass a text string representation
of the expression; parse = TRUE will convert it into an expression for
you. There was no need to parse the second geom_text() call in the
second example because there was nothing that needed to be parsed by
plotmath. Had you used <= instead, that would have been a different
story.

Dennis
Reply all
Reply to author
Forward
0 new messages