Line break in expression()?

28,099

Solution 1

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

Solution 2

You are going to need to use something else. I was directed to use mtext and bquote when I was stuck on a similar 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 )

In the above example, title (thanks to @hadley) can be simplified to

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

enter image description here

Share:
28,099

Related videos on Youtube

Jen Bohold
Author by

Jen Bohold

Interested in Risk Management, Finance, trading and statistics.

Updated on July 09, 2022

Comments

  • Jen Bohold
    Jen Bohold almost 2 years

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

    or

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

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

  • Jen Bohold
    Jen Bohold almost 11 years
    thanks for your answer, there is only one problem left: The delta and the beta are large in a vertical way, so the hat is out of the range. I.e. the border above is kind of cutting the hat. How can I change this? So move the title down a little bit?
  • IRTFM
    IRTFM almost 11 years
    There is an oma argument to par::: opar <- par(oma=c(3,3,3,3)) ....do your plotting ...par(opar) Creates extra room around the plot.
  • Simon O'Hanlon
    Simon O'Hanlon almost 11 years
    @JenBohold thanks, alas, I cannot take the credit, I learnt this from agstudy!
  • Jen Bohold
    Jen Bohold almost 11 years
    when I do opar <- par(oma=c(3,3,3,3)) and then par(opar) and then the hist(...) it does not change anything?
  • IRTFM
    IRTFM almost 11 years
    As I said... there are more complicated solutions.
  • IRTFM
    IRTFM almost 11 years
    You got the order wrong opar<-par(.) then hist(.), then par(opar). par(opar) is restoring the default settings you "saved to the side" with the first par call.
  • Simon O'Hanlon
    Simon O'Hanlon almost 11 years
    @DWin I personally find this more intuitive than the expression in atop. One bquote per line.
  • Jen Bohold
    Jen Bohold almost 11 years
    Thanks for your answer and I do not want to keep kicking on this point, BUT: This does not help, because the margin increase but the hat is still cutted off!
  • IRTFM
    IRTFM almost 11 years
    Different plotting devices I guess. I tested it on my screen device but I hadn't had the problem in the first place. What happens if you print to a pdf device?
  • Simon O'Hanlon
    Simon O'Hanlon almost 11 years
    @jenbohold that is entirely your choice (if you want to accept any answer!)
  • IRTFM
    IRTFM almost 11 years
    Don't worry about that. It's not going to insult either of us.
  • Jen Bohold
    Jen Bohold almost 11 years
    I don't know how to print to a pdf device? I am just running the code and R Graphics: Device 2 (ACTIVE) opens. No matter if I use fullscreen or not the problem is the same.
  • Jen Bohold
    Jen Bohold almost 11 years
    I am just asking, because then your answer is perfect and there is no need using a more complicated version, although the version with bquote is also an excellent solution.
  • hadley
    hadley almost 11 years
    You can simplify title a little to as.list(expression(paste("Histogram of " , hat(mu)), "Bootstrap samples, Allianz"))
  • Etienne Low-Décarie
    Etienne Low-Décarie over 10 years
    Any similar solution for within a ggplot?
  • IRTFM
    IRTFM about 8 years
    If you cannot find another question after searching that answers the ggplot question then you should post another question. (I suspect it's been asked and answered.)