如何在与`text`一起使用的`bquote`表达式中换行? [英] How to have a new line in a `bquote` expression used with `text`?

查看:196
本文介绍了如何在与`text`一起使用的`bquote`表达式中换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 bquote 环境中换行,该怎么办?

I want to have a new line in my bquote envrionment, how can I do this?

我代码:

test<-c(1,2,3,4,4.5,3.5,5.6)
test2<-0.033111111
plot(test,c(1:length(test)))

segments(4,0,4,23,col="red",lwd=2)
text(5, 4.5, labels = bquote(Qua[0.99] == .(round(test2,4))),col="red", cex = 1.4)

我想在等号后添加新行,所以应该这样:

And I want to have a new line after the equal sign, so this should give:

VaR_0.99 =

0.03311

and not

    VaR_0.99 = 0.03311

我用线尝试了一下,但是没有用:

I tried it with lines, but it did not work:

    test<-c(1,2,3,4,4.5,3.5,5.6)
    test2<-0.033111111
    lines<-list(bquote(Qua[0.99] == ),bquote(.(round(test2,4))))
    plot(test,c(1:length(test)))

    segments(4,0,4,23,col="red",lwd=2)
    text(5, 4.5, labels =lines ,col="red", cex = 1.4)


推荐答案

上面提到了一个错误。 R表达式会被解析,因此它们在语法上必须有效。由于 ==是两个参数的函数,因此它不能是表达式中的最后一项。 phantom 函数用作非打印的占位符。也可能是空字符值( )。由于没有需要评估的外部值,因此我只使用了 expression()而不是 bquote()作为表达式列表中的第一个参数。

One of the errors was noted above. R expressions get parsed so they need to be syntactically valid. Since "==" is a two-argument function it cannot be the final item in an expression. The phantom function serves as a placeholder that is non-printing. Could also have been an empty character value (""). Since there was no "outside" value that needed to be evaluated, I just used expression() rather than bquote() for the first argument in the expression list.

另一个是语义错误而不是句法错误。您需要为第二个 bquote 表达式指定一个明确的y位置。文本的几乎所有重要参数都具有矢量功能,但对于y值似乎没有隐式的向上(或向下)索引:

The other one was more a semantic error than a syntactic one. You need to give an explicit y location for the second bquote expression. Pretty much all of the important arguments of text are vector-capable but there doesn't appear to be implicit up-(or down-)indexing for the y values:

test <- c(1, 2, 3, 4, 4.5, 3.5, 5.6)
test2 <- 0.033111111
lines <- c( expression(Qua[0.99] == phantom(0)) ,
           bquote(.(round(test2,4)))
          )
plot(test,c(1:length(test)))

segments(4,0,4,23,col="red",lwd=2)
text(5, c(4.5, 4), labels =lines ,col="red", cex = 1.4)

我使用了顶部过去曾提出过自己的建议,甚至曾在Rhelp上提出过建议,但我认为上述方法可以更好地归纳为三个或更多表达式,并可以更好地控制位置。 atop 也会默默地减小字体大小,因此,如果您要执行三个表达式任务的嵌套顶部路线,则可能需要 atop(atop(。 ..,...),atop(...,phantom())来保持大小一致。

I have used the atop suggestion myself in the past, even suggesting it on Rhelp, but I think the approach above generalizes better to three or more expressions and allows more control over positioning. atop also silently reduces font sizes, so if you went the nested atop route for a three expression task, it might need to be atop( atop(..., ...), atop(..., phantom() ) to keep the sizes uniform.

这篇关于如何在与`text`一起使用的`bquote`表达式中换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆