I want to add brackets [] around an expression in ggplot. In my code I want the brackets around the "O2" expression. I have tried this code for naming the y axis of my plot. But it does not work, where am I going wrong here?
scale_y_continuous(name = bquote('[O'[2], ']'[crit]~(mg~O[2]/L^-'1'))
I get the error "Error in as.environment(where) : invalid object for 'as.environment'"
Thank you in advance.
CodePudding user response:
The bquote
can be fixed i.e.
...
scale_y_continuous(name = bquote("["*O[2]*"]"[crit]~(mg~O[2]/L^-1)))
-testing
plot(1, 1, main = bquote("["*O[2]*"]"[crit]~(mg~O[2]/L^-1)))
-output
CodePudding user response:
I managed to fix it with "expression" and "paste" but I'm still interested to know how I can do this with "bquote"
scale_y_continuous(name = expression(paste("[O"[2], "]"[crit]~(mg~O[2]/L^-'1')))
CodePudding user response:
I'm not sure why are you using bquote
here since you don't seem to be inserting any values, a regular expression
or quote
should be just fine. Also if you check the ?plotmath
help page, you'll see the group()
and bgroup()
functions where are usefult for surrounding elements in delimiters. You can use
scale_y_continuous(name = expression(group("[", O[2], "]")[crit]~(mg~O[2]/L^-'1')))
This will also with with bquote
too if you really want to use that instead.