R 在plotmath表达式中使用变量

R 在plotmath表达式中使用变量,r,plotmath,R,Plotmath,我试图将回归(即R2)的结果放在图形中,但似乎不知道如何从表达式中调用变量(它粘贴了变量名) 这是我的密码 R2Val <- signif(summary(sMod_pre90)$r.squared[1], 2) text(92, 4, expression(paste(R^2, " = ", R2Val)), adj = 0, cex = 0.85) R2Val我已经设法使用替代函数将其组合在一起 R2Val <- signif(summary(sMod_pre90)$r.squ

我试图将回归(即R2)的结果放在图形中,但似乎不知道如何从表达式中调用变量(它粘贴了变量名)

这是我的密码

R2Val <- signif(summary(sMod_pre90)$r.squared[1], 2)
text(92, 4, expression(paste(R^2, " = ", R2Val)), adj = 0, cex = 0.85)

R2Val我已经设法使用替代函数将其组合在一起

R2Val <- signif(summary(sMod_pre90)$r.squared[1], 2)
text(92, 4, substitute(R^2 ~ "=" ~ R2Val), adj = 0, cex = 0.85)
R2Val使用
bquote()
。下面是一个使用虚拟数据的示例:

set.seed(1)
DF <- data.frame(A = rnorm(100), B = rnorm(100))
mod <- lm(B ~ A, data = DF)
R2Val<-signif(summary(mod)$r.squared[1], 2)
另一种可能的解决方案是
替换
,其工作原理类似:

plot(B ~ A, data = DF)
text(1.5, 2, labels = substitute(R^2 == A, list(A = R2Val)), adj = 0, cex = 0.85)

要组合
substitute()
paste()
,我使用以下代码

waic <-1;
chisquare <-2;
plot(x=1:2,y=1:2, 
     main =  substitute(paste(
    "chi^2 goodness of fit ", 
       chi^2*(D/theta) , "=",  chisquare ,
       ".  WAIC =",waic),


list(chisquare=chisquare,waic=waic)  
)
)

不起作用的waic。缺少一个参数来告诉
substitute
要替换哪些变量。看我的答案。我已经用上面的解决方案成功地将文本粘贴到了图表中;它似乎代替了变量调用,同时按其应该的方式计算绘图数学。我不知道为什么它对你不起作用。好吧,我已经在两台机器上检查了这个问题,这两台机器在Linux上都有R2.11补丁和R2.12补丁,你的答案在这两台机器上都不起作用。
R2Val
在绘图上按字面打印,而不是解释。我认为
?substitute
非常清楚,除非您为
env
参数提供一些内容,否则在本例中不会发生替换。我已经在我的linux机器上测试了上面的代码,我想我已经找到了区别所在。直接在控制台中运行上述代码会导致按字面形式打印变量(即R2Val),但是,当我之前运行代码时,它被包装在一个函数中。如果将模型开发和打印代码包装到函数中,则会得到所需的结果。在这种情况下,必须暗示环境。我接受你的回答,因为这是最好的方式。我希望这能把事情弄清楚。
waic <-1;
chisquare <-2;
plot(x=1:2,y=1:2, 
     main =  substitute(paste(
    "chi^2 goodness of fit ", 
       chi^2*(D/theta) , "=",  chisquare ,
       ".  WAIC =",waic),


list(chisquare=chisquare,waic=waic)  
)
)