Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression and new line in plot labels [duplicate]

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:

enter image description here

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?

like image 848
Marta Cz-C Avatar asked Dec 12 '13 16:12

Marta Cz-C


People also ask

How do you add labels to a plot in Matplotlib?

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.

How do I make a label/title using the expression?

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.

How do you label axis labels in R plots?

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.

How to make a newline in plotmath?

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.


1 Answers

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)))
like image 98
Konrad Rudolph Avatar answered Oct 03 '22 02:10

Konrad Rudolph