Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting axis labels with expressions

Tags:

graph

r

axis

I have a plot with a long label containing an expression and I want to split it on two lines.
Adding a "\n" inside the expression the result is not as expected.

ylabel <- expression("A very long label with text and \n 
expression"*(alpha+beta) [ij]*"  A very long label with text and expression")
curve(x^3 - 3*x, -2, 2, xlab=xlabel)

Any help would be appreciated. Thanks

like image 309
Marco Sandri Avatar asked Mar 24 '26 22:03

Marco Sandri


1 Answers

Here is another solution, relying on atop as did @AndresT in his edit. Note that we cannot use control character like \n in an expression, which explains why using something like expression(paste("...\n", alpha[i], "....")) would not produce the desired output.

xlabel <- expression(atop("A very long label with text and expression", 
                          paste((alpha+beta)[ij], "   A very long label ...")))
curve(x^3 - 3*x, -2, 2, sub=xlabel, xlab="")

Note that I used sub instead of xlab to avoid collision with x tick marks.

enter image description here

like image 118
chl Avatar answered Mar 26 '26 11:03

chl