We can use subscript or superscript in a plot title in R using the following
plot(1,1, main=expression('title'^2)) #superscript
plot(1,1, main=expression('title'[2])) #subscript
However, what if I want to use a string variable in expression. For example,
my_string="'title'^2"
plot(1,1, main=expression(my_string))
Clearly, this doesn't work and the plot title just becomes my_string rather than title^2.
Is it possible to use a string variable inside expression?
Thanks, Brij.
In order to make an expression from a string, you need to parse it. Try this
my_string <- "'title'^2"
my_title <- parse(text=my_string)
plot(1,1, main=my_title)
If you just wanted to swap out certain parts of the expression with a string value, you can use something like bquote
my_string <- "title"
my_title <- bquote(.(my_string)^2)
plot(1,1, main=my_title)