How to have a new line in a `bquote` expression used with `text`?

后端 未结 3 745
滥情空心
滥情空心 2020-12-17 19:59

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

my code:

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


        
相关标签:
3条回答
  • 2020-12-17 20:36

    For instance, using 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)
    

    enter image description here

    0 讨论(0)
  • 2020-12-17 20:37

    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.

    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)
    

    enter image description here

    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.

    0 讨论(0)
  • 2020-12-17 20:44

    I wanted this but left aligned. After searching for solutions I ended up using two text() calls set up to put the text 0.25 inch apart and 0.25 in from top regardless of the plot dimensions.

    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

    0 讨论(0)
提交回复
热议问题