Substrings and superscripts "-" or "+" with ggplot2 axis labels? (ionic chemical notation)

I got this plot using the code below

enter image description here

On my graph, I want NO3 to have a negative β€œ-” as a superscript as shown below

enter image description here

In the x axis label, I could not use the negative sign only as superscript for 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})) 

Are there any suggestions on how to change the label to only have a negative sign without 1?

+12
source share
1 answer

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 that from a scientific point of view, it is more accurate to use the %.% Operator between blocks:

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

textstyle should not allow the size of superscript to be reduced. I'm also not sure why you have a " " between two tildes. You can link a whole bunch of tildes to increase the "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 conspiracy tip: quoting numbers is a way around the documented difficulty of creating italic numbers using a conspiracy. (Using italic(123) fails, but italic("123") does.)

+15
source

Source: https://habr.com/ru/post/1240984/


All Articles