26

I got this plot using the code below

enter image description here

In my plot, I want the NO3 to have negative sign"-" as superscript like below

enter image description here

In the label of x axis, I couldn't use negative sign only as a superscript to NO3 so I had to use -1 as shown below

x <- seq(0,2*pi,0.1)
y <- sin(x)

df <- data.frame(x, y)
ggplot(df, aes(x=x, y=y))+
geom_point(size=4)+
labs(x=expression(Production~rate~" "~mu~moles~NO[3]^{-1}-N~Kg^{-1}),
     y=expression(Concentration~mg~L^{-1})) 

Any suggestions on how to change the label to have a negative sign only without 1?

shiny
  • 3,380
  • 9
  • 42
  • 79
  • 3
    Just use a string value rather than a numeric one: `NO[3]^{"-"}` – MrFlick Jan 20 '16 at 05:35
  • 2
    And there's yet another strategy. Use an empty character after a (unary) minus. Has the advantage that all the minuses then look alike: `NO[3]^{-""} – IRTFM Jan 27 '18 at 19:35

1 Answers1

29

Try quoting the minus sign after the superscript operator:

ggplot(df, aes(x=x, y=y))+
geom_point(size=4)+
labs(x=expression(Production~rate~" "~mu~moles~NO[3]^{"-"}-N~Kg^{-1}),
     y=expression(Concentration~mg~L^{-1})) +
theme(legend.title = element_text(size=12, face="bold"),
      legend.text=element_text(size=12),
      axis.text=element_text(size=12),
      axis.title = element_text(color="black", face="bold", size=18))

I think it looks more scientifically accurate to use the %.% operator between units:

+ labs(x=expression(Production~rate~" "~mu~moles~NO[3]^{textstyle("-")}-N %.% Kg^{-1}),
     y=expression(Concentration~mg~L^{-1})) +

textstyle should keep the superscript-ed text from being reduced in size. I'm also not sure why you have a " " between two tildes. You can string a whole bunch of tildes together to increase "spaces":

ggplot(df, aes(x=x, y=y))+
geom_point(size=4)+
labs(x=expression(Production~rate~~~~~~~~~~~~mu~moles~NO[3]^{textstyle("-")}-N %.% Kg^{-1}),
     y=expression(Concentration~mg~L^{-1})) +
theme(legend.title = element_text(size=12, face="bold"),
      legend.text=element_text(size=12),
      axis.text=element_text(size=12),
      axis.title = element_text(color="black", face="bold", size=18))

enter image description here

And a bonus plotmath tip: Quoting numbers is a way to get around the documented difficulty in producing italicized digits with plotmath. (Using italic(123) does not succeed, ... but italic("123") does.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • How would this work if you wanted to have the units contained within parentheses? Such as "Concentration (mg L^-1)" rather than "Concentration mg L^-1" – tassones Feb 24 '21 at 19:50
  • 1
    Parentheses could be inside text-quotes so: `expression(Concentration~"("*mg~L^{-1}*")")`. seems less elegant. Better I think is `expression(Concentration~(mg%.%L^{-1}) )`. Note that `*` is used when no space is desired between non-plotmath items. As a matter of style I would prefer using a 'cdot' between unit designators: `expression(Concentration~(mg%.%L^{-1}))` – IRTFM Feb 25 '21 at 00:22
  • I ended up using ```labs(y=expression(paste("Concentration (mg ", L^-1,")")))``` – tassones Feb 25 '21 at 01:39