表达式()中的换行符?

Jen*_*old 39 plot r line-breaks title plotmath

我在R中有以下直方图:

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

标题太长了,所以我想换线.根据这个线程,我试过

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")))
Run Code Online (Sandbox Code Playgroud)

要么

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")))
Run Code Online (Sandbox Code Playgroud)

但两者都不起作用,我怎样才能在paste()中获得换行符?

42-*_*42- 39

您可以在常规中轻松使用换行符paste,但这是plotmath paste(实际上是一个不同的函数,也没有'sep'参数),而(long) ?plotmath页面明确告诉您无法完成.那么解决方法是什么?使用plotmath函数atop是一个简单的选项:

expression(atop("Histogram of "*hat(mu), Bootstrap~samples*','~Allianz))
Run Code Online (Sandbox Code Playgroud)

这将以逗号为中心并以plotmath表达为中心.有更复杂的选项.

这说明了绘制图形文件.具有讽刺意味的是,第一次努力给了我一个显示器确实你的问题与'帽子'(是那些回旋??)被切断,这显示了如何增加利润.上边距可能是第三个数字,所以c(3,3,8,0)可能更适合你:

 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()
Run Code Online (Sandbox Code Playgroud)


Sim*_*lon 20

你将需要使用其他东西.我被指示使用mtext,bquote当我遇到类似问题时.

alpha = rnorm(1e3)
hist(alpha,cex.main=2,cex.axis=1.2,cex.lab=1.2,main=NULL )

title <- list( bquote( paste( "Histogram of " , hat(mu) ) ) ,
               bquote( paste( "Bootstrap samples, Allianz" ) ) )


mtext(do.call(expression, title ),side=3, line = c(1,-1) , cex = 2 )
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,title(感谢@hadley)可以简化为

title <- as.list(expression(paste("Histogram of " , hat(mu)), "Bootstrap samples, Allianz"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @DWin 我个人觉得这比 `atop` 中的表达式更直观。每行一个 `bquote`。 (2认同)
  • 您可以将`title`简化为`as.list(表达式(粘贴("直方图",帽子(mu)),"Bootstrap样本,Allianz")) (2认同)
  • ggplot 中的任何类似解决方案? (2认同)