Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line break in expression()?

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

like image 831
Jen Bohold Avatar asked Aug 14 '13 16:08

Jen Bohold


People also ask

How do you force a line break?

Simply place the tag wherever you want to force a line break. Since an HTML line break is an empty element, there's no closing tag. Below is an HTML file with a <p> and <br> element.

What does line break mean in coding?

Also called "EOL" (end-of-line), "newline," and "hard return," a line break code is generated when the Enter key is pressed, When typing a command on a command line, pressing Enter executes the command. When typing text, pressing Enter signifies the end of the paragraph, and subsequent text goes to the next line.

How do you represent a line break in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


1 Answers

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() 
like image 114
IRTFM Avatar answered Sep 23 '22 01:09

IRTFM