Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot an expression which symbols name are stored in a vector

Tags:

r

I have a character vector which contains Greek letters.

labNames <- c("rho x alpha", "z x rho", "beta x rho")

I want to see these labels on X axis, referring to the object labNames. The result must be as if I used expression(rho x alpha). Typing expression(labNames[1]) is wrong, but I'm not able to figure out how to solve it. Please look at the following example:

plot(c(2,5,8), 1:3, axes=F, pch=19, xlab="", ylab="")
axis(2, 1:3, labels = c(expression(labNames[1]), expression(labNames[2]), expression(labNames[3])), las=2, tck = FALSE, lty = 0)
like image 921
Rob Avatar asked Mar 17 '23 05:03

Rob


1 Answers

You need to format the expressions properly:

labNames <- c("rho x alpha", "z x rho", "beta x rho")
labNames <- gsub(' ', '~', labNames)

and then you can use parse

plot(c(2,5,8), 1:3, axes=F, pch=19, ann = FALSE)
axis(2, 1:3, labels = parse(text = labNames), las=2, tck = FALSE, lty = 0)

enter image description here

like image 51
rawr Avatar answered Apr 07 '23 09:04

rawr