expression()中的换行符? [英] Line break in expression()?

查看:428
本文介绍了expression()中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中具有以下直方图:

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("\n") Bootstrap samples, Allianz")
  )
)

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

但是两者都不起作用,如何在paste()中换行?

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

推荐答案

您可以轻松地在常规paste中使用换行符,但这是plotmath paste(实际上是另一个函数,也没有'sep'参数)并且(长)?plotmath页专门告诉您它无法完成.那么解决方法是什么?使用plotmath函数atop是一个简单的选项:

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.

这说明了绘图到图形文件.具有讽刺意味的是,我的第一笔努力给了我一个展示,确实显示了您的问题:帽子"(是抑扬音?)被切断了,这显示了如何增加利润率.最高边距可能是第三个数字,因此c(3,3,8,0)可能更适合您:

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()

这篇关于expression()中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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