I am creating multiple plots with parameter names in the ggtitle. Some of these have subscripts, superscripts or other plotmath characters. My parameters are stored in a param-list and are presented as strings in my ggplot function that I am looping into a lapply plotlist. I basically just want to insert "NO[3] (mg/l)", "NH[4]^+ (µg/l)" etc from the param-list into my ggtitle(param) to create a title with these parameter names. At the same time I wish to have all [n] converted to subscripts, and all ^n converted to superscripts.
This title is not well formatted
I have read about bquote and expression, but these assume I have an expression. Even if I create expressions from the strings, the bquote cannot handle the resulting name. I was hoping to be able to transform "NO[3] (mg/l)" into NO[3]~(mg/l) or something similar and then use bquote or expression. Is there a way to do this by creating smarter param-combinations that can be easily parsed (something like [[param]] in aes), or do I have to handle the parameter strings by seperating all problematic characters and then combining again with bquote or expression?
Grateful for any advice! :)
Thank you for suggesting filters. I ended up just gsub:ing blanks for ~ and comma's for points, and then parsing the parameter strings. it ended upp just fine without having to use more filters.
This was my final code, and param_exp could then be used within ggtitle:
if (grepl("\\[|\\^", param)) { #only use if [ or ^ is detected
param_exp <- parse(text=gsub(" ", "~", gsub(",", "." ,param)))
} else {
param_exp = param
}