How to convert string to math expression in R?

I have line vectors, say c("E^A","S^P","lambda","T","E^Q","E^Q","AT"), and I want to build them as an x-axis label using a math expression. (I believe that I wrote them in a mathematical expression format, but with a 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" and not a mathematical interpretation of the string, and I assume because they are not considered as mathematical symbols.

How can I get the math marking? Thank!

+5
source share
1 answer

In general, use expression(see ?plotMath):

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

, "E ^ A" .

, 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

+7

All Articles