【问题标题】:How to have a new line in a `bquote` expression used with `text`?如何在与`text`一起使用的`bquote`表达式中换行?
【发布时间】:2013-03-17 19:27:16
【问题描述】:

我想在我的bquote 环境中添加一个新行,我该怎么做?

我的代码:

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)

我想在等号后换行,所以应该给出:

VaR_0.99 =

0.03311

and not

    VaR_0.99 = 0.03311

我用线条试了一下,还是不行:

    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)

【问题讨论】:

  • 你读过thisthis吗?
  • 我用列表试过了,但是没有用! @plannapus
  • 您不能有尾随 "=="。需要在==之后添加一些东西:phantom(0)`或" "

标签: r data-visualization plotmath


【解决方案1】:

我想要这个但左对齐。在搜索解决方案后,我最终使用了两个 text() 调用,设置为将文本分开 0.25 英寸,距离顶部 0.25 英寸,无论绘图尺寸如何。

plot(1:100)
r2 <- 40.2
rmse <- 0.6
ll = bquote(paste(italic(R)^2," = ",.(r2),"  RMSE = ",.(rmse)))
val=0.25*diff(par('usr')[3:4])/par('pin')[2]
text(par('usr')[1], par('usr')[4] - val, pos=4, labels=ll)
text(par('usr')[1], par('usr')[4] - 2*val, pos=4, labels="Model A")

Image produced by code

【讨论】:

    【解决方案2】:

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

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

    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)
    

    我自己过去曾使用过atop 建议,甚至在 Rhelp 上也建议过,但我认为上述方法可以更好地推广到三个或更多表达式,并允许对定位进行更多控制。 atop 也默默地减小了字体大小,所以如果你为三个表达式任务走嵌套的顶部路线,它可能需要atop( atop(..., ...), atop(..., phantom() ) 以保持大小一致。

    【讨论】:

      【解决方案3】:

      例如,使用atop:

      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(atop(Qua[0.99] == phantom(), .(round(test2,4)))),
           col="red", cex = 1.4)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-08
        • 2017-02-28
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 2022-01-20
        • 1970-01-01
        • 2018-09-01
        相关资源
        最近更新 更多