Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate strings and expressions in a plot's title

How can I combine text and math expressions in a plot's title. If I use paste the expression is converted to character. For example I want something like this as a title

$ARL_1$ curve for $S^2$

Thank you

like image 382
Brani Avatar asked Nov 29 '10 09:11

Brani


3 Answers

You want to read ?plotmath to see how to do this sort of thing. Here is an example:

plot(1:10, main = expression(ARL[1] ~ "curve for" ~ S^2))

The [.] is subscript, whilst ^ gives superscript. The ~ spaces out the parts of the expression as if there were literal spaces.

Edit: normally I would have done:

plot(1:10, main = expression(ARL[1] ~ curve ~ for ~ S^2))

but that throws an error because for is being interpreted as the start of a for() loop call.

like image 187
Gavin Simpson Avatar answered Oct 21 '22 16:10

Gavin Simpson


You can also use bquote(paste(...)), which is a little more flexible than expression: you can include variable values (say, the value of x) in the labels with .(x). For example:

x<- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=",.(x))))
like image 41
fabians Avatar answered Oct 21 '22 15:10

fabians


You can also use latex2exp::TeX to convert TeX to expressions on the fly:

plot(cars, main = TeX("$ARL_1$ curve for $S^2$"))
like image 35
loki Avatar answered Oct 21 '22 16:10

loki