Search code examples
rggplot2axis-labels

How to superscript a - (minus symbol) in a legend title with qpolot in ggplot2 in R


I'm trying to superscript a '-' straight after a subscript 4 in an axis title in ggplot2 using bquote. I'm so close! The following gives me everything EXCEPT a small, superscript - straight after the subscript 4:

qplot(uptake, data = CO2) +  
ylab(bquote('Membrane available NH '[4]*'-N (  '*mu*'g' ~ resin^-1* ~ 14~day^-1*')'))

I've tried playing around the ^, for example:

qplot(uptake, data = CO2) +  
ylab(bquote('Membrane available NH '[4]*^-'-N (  '*mu*'g' ~ resin^-1* ~ 14~day^-1*')'))

But get error messages such as:

Error: unexpected '^' in "qplot(uptake, data = CO2) +  
ylab(bquote('Membrane available NH '[4]*^"

Clearly I'm not expressing the ^ correctly.

Any help would be greatly appreciated.

Many thanks!


Solution

  • Is this what you want?

    library(ggplot2)
    qplot(uptake, data = CO2) +  
      ylab(expression(Membrane~available~NH[4^~{"-"}]-N~(mu~g~resin^{-1}~14~day^{-1})))+
      theme(axis.title.x = element_text(color="black", face="bold", size= 12, margin=margin(10,0,0,0)),
           axis.title.y= element_text(color="black", face="bold", size= 18, margin=margin(0,10,0,0)))
    

    enter image description here