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?
Create your expression () first and save the result to a named object to help keep yourself organised. You can use the title () command to add titles to the main marginal areas of an existing plot. In general, you’ll use xlab and ylab elements to add labels to the x and y axes.
If you are using expression () to make a label/title then save the expression () result as a named object, which is easier to use in the subsequent command (s) that use them. The title () command has an additional “trick” up its sleeve, the line parameter. This allows you to select a position for the title (s) in lines from the edge of the plot.
These are some notes about axis labels in R plots, particularly how you can use superscript, bold and so on. The labelling of your graph axes is an important element in presenting your data and results. You often want to incorporate text formatting to your labelling. Superscript and subscript are particularly important for scientific graphs.
A fast solution is to add some spaces before the word "first". Since plotmath does not support newlines, you can use mtext to create your lines one by one like this: Not the answer you're looking for? Browse other questions tagged r plot expression newline or ask your own question.
You can introduce a line break inside an expression:
bquote(atop("first line",
"second line" ~ x ^ 2))
(I’m using bquote
rather than expression
here – both work in this case.)
Execute demo(plotmath)
for more information and look at the documentation for atop
.
boxplot
apparently has some trouble interpreting expressions in its title. A simple fix is to plot the title separately:
boxplot(data, main = '')
title(bquote(atop("first line", "second line" ~ x ^ 2)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With