Cannot concatenate more than 3 elements in an expression for ggplot2's geom_text
I have a data frame for which I'm computing a linear model and would like to include the correlation coefficient and its significance using geom_text.
structure(list(ppno = c(1L, 1L, 1L, 10L, 10L, 10L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L,
8L, 8L, 9L, 9L, 9L), light.color = structure(c(1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("B", "IR",
"IR+B"), class = "factor"), session = c(2L, 1L, 3L, 2L, 3L, 1L,
1L, 3L, 2L, 3L, 2L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 3L, 2L,
1L, 3L, 1L, 3L, 2L, 3L, 2L, 1L), time = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("pre",
"post"), class = "factor"), pre.pri.s = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), pre.pri.r = c(8L, 4L, 6L,
2L, 2L, 4L, 10L, 12L, 9L, 24L, 16L, 15L, 15L, 15L, 15L, 3L, 5L,
7L, 13L, 11L, 12L, 16L, 15L, 14L, 21L, 5L, 8L, 1L, 0L, 0L), pre.nwc = c(5L,
2L, 4L, 2L, 2L, 4L, 10L, 10L, 9L, 11L, 10L, 11L, 12L, 11L, 11L,
3L, 5L, 6L, 9L, 11L, 12L, 12L, 11L, 10L, 11L, 5L, 8L, 1L, 0L,
0L), pre.ppi = structure(c(3L, 2L, 2L, 1L, 1L, 2L, 2L, 3L, 2L,
3L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, NA, 2L, 2L, 3L, 3L, 3L, 4L,
2L, 3L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", "NULL"), class = "factor"),
pre.pri.nwc = c(1.6, 2, 1.5, 1, 1, 1, 1, 1.2, 1, 2.18181818181818,
1.6, 1.36363636363636, 1.25, 1.36363636363636, 1.36363636363636,
1, 1, 1.16666666666667, 1.44444444444444, 1, 1, 1.33333333333333,
1.36363636363636, 1.4, 1.90909090909091, 1, 1, 1, NaN, NaN
), post.pri.s = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), post.pri.r = c(4L, 4L, 7L, 0L, 0L, 4L,
3L, 8L, 7L, 16L, 12L, 19L, 6L, 10L, 4L, 1L, 3L, 0L, 3L, 11L,
15L, 8L, 9L, 9L, 8L, 4L开发者_StackOverflow中文版, 3L, 0L, 0L, 0L), post.nwc = c(4L,
3L, 4L, 0L, 0L, 3L, 3L, 8L, 7L, 10L, 9L, 15L, 5L, 9L, 4L,
1L, 3L, 0L, 3L, 8L, 13L, 8L, 9L, 9L, 8L, 4L, 3L, 0L, 0L,
0L), post.ppi = structure(c(2L, 2L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 2L, 5L, 1L, 1L, NA, 3L, 2L, 1L, 1L,
2L, 3L, 2L, 2L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4",
"NULL"), class = "factor"), post.pri.nwc = c(1, 1.33333333333333,
1.75, NaN, NaN, 1.33333333333333, 1, 1, 1, 1.6, 1.33333333333333,
1.26666666666667, 1.2, 1.11111111111111, 1, 1, 1, NaN, 1,
1.375, 1.15384615384615, 1, 1, 1, 1, 1, 1, NaN, NaN, NaN),
delta.pri.r = c(4, 0.1, -1, 2, 2, 0.1, 7, 4, 2, 8, 4, -4,
9, 5, 11, 2, 2, 7, 10, 0.1, -3, 8, 6, 5, 13, 1, 5, 1, 0.1,
0.1), delta.nwc = c(1, -1, 0.1, 2, 2, 1, 7, 2, 2, 1, 1, -4,
7, 2, 7, 2, 2, 6, 6, 3, -1, 4, 2, 1, 3, 1, 5, 1, 0.1, 0.1
), delta.pri.nwc = c(-0.6, -0.666666666666667, 0.25, NaN,
NaN, 0.333333333333333, 0.1, -0.2, 0.1, -0.581818181818182,
-0.266666666666667, -0.0969696969696969, -0.05, -0.252525252525252,
-0.363636363636364, 0.1, 0.1, NaN, -0.444444444444444, 0.375,
0.153846153846154, -0.333333333333333, -0.363636363636364,
-0.4, -0.90909090909091, 0.1, 0.1, NaN, NaN, NaN), delta.vas = c(4.081632,
-43.877544, -8.163264, -2.040816, 0.510204, 9.183672, 8.163264,
8.163264, 11.224488, 0, -14.285712, -11.224488, 19.387752,
0, 26.530608, 2.040816, 10.20408, 11.224488, 42.346932, -10.20408,
-28.06122, 11.224488, 5.612244, 21.428568, 22.448976, 0,
23.469384, 0.510204, -1.020408, 0)), .Names = c("ppno", "light.color",
"session", "time", "pre.pri.s", "pre.pri.r", "pre.nwc", "pre.ppi",
"pre.pri.nwc", "post.pri.s", "post.pri.r", "post.nwc", "post.ppi",
"post.pri.nwc", "delta.pri.r", "delta.nwc", "delta.pri.nwc",
"delta.vas"), row.names = c(NA, -30L), class = "data.frame")
Using this code for the plot.
p <- ggplot(data=mpq.vas, mapping=aes(x=delta.vas, y=delta.pri.r,
colour=light.color)) +
geom_point() +
geom_smooth(aes(group=1), method="lm", size=1, colour="black")
#
# Clean up the basics.
pp <- p + geom_hline(yintercept=0, colour="grey60") +
geom_vline(xintercept=0, colour="grey60") +
scale_colour_manual(name="Treatment\ncolor", values=cols) +
scale_x_continuous(name=
expression(paste(Delta, " VAS pain [t(0) - t(60)]")))+
scale_y_continuous(name=expression(paste(Delta, "PRI(r) [pre - post]")))
#
# Add correlation info.
val <- cor.test(mpq.vas$delta.vas, mpq.vas$delta.pri.r)
When I then try to add the correlation coefficient somewhere in the text, I get an error about an unexpected symbol at the location of the Q in the label.
pp + geom_text(aes(x=20, y=-5, label=paste("italic(r) ==", 3, "Q", sep=" ")),
parse=TRUE, colour="black")
(yes, I know a correlation of 3 is impossible, just an example).
I would like to do:
pp + geom_text(aes(x=20, y=-5, label=paste("italic(r) ==", round(val$estimate, digits=2), "\np < 0.0001", sep=" ")), parse=TRUE, colour="black")
But this generates the same error, now at the \n thingy. What am I doing wrong?
pp + geom_text(aes(x=20, y=-5,
label=paste("list(italic(r) ==", round(val$estimate, digits=2), ", p < 0.0001)")),
parse=TRUE, colour="black")
The key is that the label argument is parsed if parse==TRUE, this means that the texts need to have a same format as in ?plotmath.
What the geom_text exactly do is like this:
expr <- parse(text=label)
and then draw text using the expr as a label. So label argument need to be a valid expression. In you example,
paste("italic(r) ==", 3, "Q", sep=" ")
is invalid expression, so
parse(text=paste("italic(r) ==", 3, "Q", sep=" "))
induces an error.
In plotmath, if you want to concat symbols, then you need to use:
paste(x, y, z)
list(x, y, z)
So if you want to simply concat, then
geom_text(foobar, label=paste("paste(italic(r) ==", 3, "Q)", sep=" "))
The first (outside) paste concats a piece of texts into one text variable. The second (inside) paste is used in plotmath process.
In my example above, I used list (see ?plotmath) instead of paste, because stats and p value is separated by `,'.
精彩评论