Subscripts and superscripts “-” or “+” with ggplot2 axis labels? (ionic chemical notation)

巧了我就是萌 提交于 2019-12-09 08:46:53

问题


I got this plot using the code below

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

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?


回答1:


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))

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.)



来源:https://stackoverflow.com/questions/34892262/subscripts-and-superscripts-or-with-ggplot2-axis-labels-ionic-chemical

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!