Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add subscript to ggplot axis title when title starts with bracket symbol

I’ve looked on SO and haven’t found this exact issue or solution for this.
I am trying to label my x-axis: [NO3 + NO2] (mu mol L ^-1); where the 3 and 2 are subscripts, the mu shows as the symbol and the -1 is a superscript.

x-axis label that I want

I think my main issue right now is that my x-axis title has to start with the "[" bracket.
I am using ggplot.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am able to get the first part "[NO3" but haven’t been able to add anything past the subscripted 3:

xlab(bquote('[NO'* "" [3]))

Can’t seem to add anything after the 3 without getting errors

I’ve tried the following:

xlab(bquote('[NO'* "" [3] '+ NO' * "" [2] '] ('))

Error: unexpected string constant in:
"  #xlab(bquote([NO[3] + NO [2]]))
  xlab(bquote('[NO'* "" [3] '+ NO'"

and

xlab(bquote('[NO'* "" [3]~ '+ NO' * "" [2] '] ('))

Error: unexpected string constant in:
"  #xlab(bquote([NO[3] + NO [2]]))
  xlab(bquote('[NO'* "" [3]~ '+ NO' * "" [2] '] ('"

and

xlab(bquote('[NO'* "" [3]~ ~'+ NO' * "" [2] '] ('))

Error: unexpected string constant in:
"  #xlab(bquote([NO[3] + NO [2]]))
  xlab(bquote('[NO'* "" [3]~ ~'+ NO' * "" [2] '] ('"

and

xlab(bquote('[NO'* "" [3]'+ NO' * "" [2] '] ('))

Error: unexpected string constant in:
"  #xlab(bquote([NO[3] + NO [2]]))
  xlab(bquote('[NO'* "" [3]'+ NO'"

and

xlab(bquote('[NO'* "" [3]}~'+ NO' * "" [2] '] ('))

Error: unexpected '}' in:
"  #xlab(bquote([NO[3] + NO [2]]))
  xlab(bquote('[NO'* "" [3]}"

and

xlab(bquote('[NO'* "" [3]~'+ NO' * "" [2] '] ('))

Error: unexpected string constant in:
"  #xlab(bquote([NO[3] + NO [2]]))
  xlab(bquote('[NO'* "" [3]~'+ NO' * "" [2] '] ('"

and variations of…

labs(x = expression([NO[3]]))

Nothing seems to work…
Any suggestions?

Additionally, any way of splitting y-axis into three lines?
I was able to get what I want for y-axis title, which I have as:

ylab(bquote('\u03b1 '^{"chla"}~'\n(g C g chla'^{"-1"}~'hr'^{"-1"}~'/ ' *mu~ 'mol quanta' ~m^{"-2"}~'s'^{"-1"}~')'))

y-axis title

Is it possible to split this into 3 lines (the \n doesn’t seem to do anything here)?

Thank you.

>Solution :

We could do it this way:

Using atop we could stack the 3 parts of the y label.

With expression we specify the text of the label for x:

The first part of the label is [NO[3]]+NO[2] with a subscripted 3 and 2, the second part of the label is (mu mol L^-1) with the mu symbol and a superscripted -1:

library(ggplot2)

ggplot(mtcars, aes(x = mpg, y = wt)) +
  geom_point() +
  ylab(expression(atop('α' ^{"chla"} ~'', atop((g*C*g*chla^{"-1"}~hr^{"-1"}), '*mu~mol~quanta~m^{"-2"}~s^{"-1"}~'))))+
  xlab(expression(atop("[NO"[3]*"+"*NO[2]*"]"*(mu*"mol L"^-1))))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading