本文介绍了无法连接ggplot2的geom_text表达式中的3个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个数据框,我正在计算一个线性模型,并希望使用geom_text包含相关系数及其显着性。 结构(列表(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,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,N A,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, ,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, bL 2,3L,1L,1L,1L),.Label = c(1,2,3,4,NULL),class =factor .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.3333333333333, 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),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,3L, ,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 .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 ),δ.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,光.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) 使用此代码绘制图。 p color = light.color))+ geom_point()+ geom_smooth(aes(group = 1),method =lm ,size = 1,color =black)##清理基础知识。 pp geom_vline(xintercept = 0,color =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])))##添加关联信息。 val< - cor.test(mpq.vas $ delta.vas,mpq.vas $ delta.pri.r) 当我尝试在文本中某处添加相关系数时,我会在标签中Q的位置出现有关意外符号的错误。 p> pp + geom_text(aes(x = 20,y = -5,label = paste(italic(r)==, 3,Q,sep =)), parse = TRUE,color =black) (是的,我知道3的关联是不可能的,只是一个例子)。 我想这样做: pp + geom_text(aes(x = 20,y = -5,label = paste(italic(r)==估计,digits = 2),\ np 但是这会产生相同的错误,现在在\ n事情。 解决方案 pp + geom_text(aes(x = 20, y = -5, label = paste(list(italic(r)==,round(val $ estimate,digits = 2),,p parse = TRUE,color =black) 关键是如果解析标签参数== TRUE,这意味着文本需要与in?plotmath具有相同的格式。 geom_text正是这样做的: expr< - parse(text = label) ,然后使用expr作为标签来绘制文本。所以标签参数需要是一个有效的表达式。在你的例子中, paste(italic(r)==,3,Q,sep =) 是无效的表达式,所以 parse(text = paste(italic(r)==,3,Q,sep =)) 导致错误。 在plotmath中,如果要连接符号,则需要使用: / p> paste(x,y,z) list(x,y,z) 所以如果你想简单地连接,那么 geom_text(foobar,label = paste(paste(italic(r)==,3,Q),sep =)) pre> 第一个(外部)粘贴将一段文本连接成一个文本变量。 在plotmath过程中使用了第二个(内部)粘贴。 在上面的示例中,我使用list(请参阅?plotmath)而不是粘贴,因为stats并且p值由`,'分隔。 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, 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, soparse(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, thengeom_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 `,'. 这篇关于无法连接ggplot2的geom_text表达式中的3个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 09:42