R – how to convert string into math expression in R

mathplotrstring

I have a vectors of strings, say c("E^A","S^P","lambda","T","E^Q","E^Q","AT"), and I want to plot them as the x axis label using math expression. (I believe I have written them in math expression format, but with quote)

When I put

text(x,par("usr")[3]-0.2,labels=substitute(A,list(A=label)),srt=20,pos=1,adj = c(1.1,1.1), xpd = TRUE,cex=0.7)

The x axis only shows "E^A","S^P","lambda","T","E^Q","E^Q","AT", not the mathematical interpretation of the strings, and I guess it's because they are not regarded as math symbols.

How can I get mathematical labeling then? Thanks so much!

Best Solution

In general, use expression (see ?plotMath):

plot(1,main=expression(E^A))

Note that the 'E^A' is not in quote marks.

To generate expressions from a character vector, use parse(text=...):

lbls <- c("E^A","S^P","lambda","T","E^Q","E^Q","AT")    
x <- 1:length(lbls)
y <- runif(length(lbls))
# I'm just going to draw labels on the (x,y) points.
plot(x,y,'n')
text(x,y, labels=parse(text=lbls)) # see the parse(text=lbls) ?

enter image description here