expression()中的换行符?

问题描述 投票:43回答:2

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

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

标题太长,所以我想换行。根据此thread,我尝试过

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()中换行?

r plot title line-breaks plotmath
2个回答
42
投票

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

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

这将在逗号处中断并使绘图仪表达式居中。提供更复杂的选项。

这说明了绘制到图形文件。具有讽刺意味的是,我的第一笔努力给了我一个展示,确实显示了您的问题:“帽子”(是抑扬音?)被切断了,这显示了如何增加利润。上边距可能是第三个数字,因此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()

20
投票

您将需要使用其他东西。当我被卡在mtext上时,被指示使用bquotesimilar problem

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 )

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

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

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9xOXNXai5wbmcifQ==” alt =“在此处输入图像描述”>“ >>

© www.soinside.com 2019 - 2024. All rights reserved.