Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

r - Line break in expression()?

I have the following histogram in R:

hist(
  alpha, cex.main=2, cex.axis=1.2, cex.lab=1.2,
  main=expression(
    paste("Histogram of ", hat(mu), ", Bootstrap samples, Allianz")
  )
)

The title is too long, so I want a line break. According to this thread I tried

hist(
  alpha, cex.main=2, cex.axis=1.2, cex.lab=1.2,
  main=expression(
    paste("Histogram of ", hat(mu), ",cat("
") Bootstrap samples, Allianz")
  )
)

or

hist(
  alpha, cex.main=2, cex.axis=1.2, cex.lab=1.2,
  main=expression(
    paste("Histogram of ",hat(mu), cat("
"),", Bootstrap samples, Allianz")
  )
)

But both do not work, how can I get a line break in paste()?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can easily use line breaks in regular paste, but this is plotmath paste (actually a different function also with no 'sep' argument) and the (long) ?plotmath page specifically tells you it cannot be done. So what's the work-around? Using the plotmath function atop is one simple option:

expression(atop("Histogram of "*hat(mu), Bootstrap~samples*','~Allianz))

This will break at the comma and center the plotmath expressions. More complicated options are available.

This illustrates plotting to a graphics file. Ironically, the first effort gave me a display that did have your problem with the 'hat' (are those circumflexes?) being cut off and this shows how to increase the margins. The top margin is probably the third number so c(3,3,8,0) might suit you better:

 pdf("test.pdf") ;  par(mar=c(10,10,10,10))
 hist(1:10,cex.main=2,cex.axis=1.2,cex.lab=1.2,
 main=expression(atop("Histogram of "*hat(mu), 
                       Bootstrap~samples * ',' ~Allianz)))
 dev.off() # don't need to restore;  this 'par' only applies to pdf()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...