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

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

问题描述

我在 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("
") Bootstrap samples, Allianz")
  )
)

hist(
  alpha, cex.main=2, cex.axis=1.2, cex.lab=1.2,
  main=expression(
    paste("Histogram of ",hat(mu), cat("
"),", 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))

这将在逗号处中断并使 plotmath 表达式居中.可以使用更复杂的选项.

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

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

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