Robert McDonald
Robert McDonald

Reputation: 1340

R: How to mix a series of variables and Greek symbols in a string

Suppose x and y are variables. I am trying to create a graph title that has a Latex equivalent of $\sigma_v =$ 3 $\rho =$ 5, where the values 3 and 5 come from R variables.

Here is the closest I have been able to come (modeled after an example in ?plotmath):

x <- 1:10
y <- 3
z <- 5
plot(x,x)
mtext(substitute(list(sigma[v],rho) == group("(",list(a,b),")"),list(a=y,b=z)))

Is it possible to have a sequence of "variable = number" strings?

Upvotes: 4

Views: 1863

Answers (2)

IRTFM
IRTFM

Reputation: 263332

The LaTex expression didn't have a comma separating the two pairs but it appears you did want one in there so see if this is a good solution:

mtext(substitute(sigma[v] == a *","~ rho == b, list(a=y,b=z)))

To comment on the notion of "concatenating expressions" raised by mcd: Valid (invisible) "concatenators" are ~ and * and the (visible) infix operators. Spaces are not valid separators (and they are ignored). You do not need to quote anything except commas, parentheses not used for visible grouping of uninterpreted sub-expressions, and carets and brackets not used for their side-effects. Sometimes it is handier to quote a long line of text because spaces are easier to type than multiple *'s or tilde's to separate non-plotmath-ish words.

Upvotes: 2

Gavin Simpson
Gavin Simpson

Reputation: 174778

You can use bquote() to do what you said you wanted (LaTeX equivalent). Here is an example:

x <- 1:10
y <- 3
z <- 5
plot(x,x)
title(main = bquote(sigma[v] == .(y) ~~ rho == .(z)))

Upvotes: 3

Related Questions