0

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.

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.

FishyFishies
  • 301
  • 3
  • 14

1 Answers1

1

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

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    oh wow...simple and worked perfectly... thank you! What does "atop" mean/do ? – FishyFishies Apr 22 '23 at 19:19
  • 1
    Please check the update. Now the second part should be also answer to give y label in 3 rows. For this `atop` function is very handy. – TarJae Apr 22 '23 at 19:43