This question already has an answer here:
- Line break in expression()? 2 answers
I want to add some subscripts and superscripts to my graph labels. I've try expression
, but it doesn't work as I wish with new lines (\n
). I've try to fix it using paste
, but it doesn't work. Below are some of my tries:
par(mfcol=c(1,3))
plot(1,1,main=expression("first line \n second line x"^2))
plot(1,1,main=expression(paste("first line \n second line", "x"^2)))
plot(1,1,main=paste("first line \n second line", expression("x"^2)))
It produces:
In first two pictures the second line is not well centered, in the third one the superscript fails. How to get both centered line and subscripts/superscripts?
A fast solution is to add some spaces before the word "first".
Since
plotmath
does not support newlines, you can usemtext
to create your lines one by one like this:You can introduce a line break inside an expression:
(I’m using
bquote
rather thanexpression
here – both work in this case.)Execute
demo(plotmath)
for more information and look at the documentation foratop
.boxplot
apparently has some trouble interpreting expressions in its title. A simple fix is to plot the title separately: