R 转换科学符号+;e至10^(y),带几何体文字上标

R 转换科学符号+;e至10^(y),带几何体文字上标,r,ggplot2,label,expression,R,Ggplot2,Label,Expression,我想在每个条的顶部添加数据标签。但是我希望它是10^格式,而不是+e。 有很多关于斧头的文献。标签无。 我的代码如下: amino.acids<-c(rep("Lysine",4),rep("Tryptophan",4),rep("Valine",4)) Source<-(rep(c("Cow","Cat","Monkey","Human"),3)) Values<-c(46846813,3186186135,6816135168,3178168,

我想在每个条的顶部添加数据标签。但是我希望它是10^格式,而不是+e。 有很多关于斧头的文献。标签无。 我的代码如下:

    amino.acids<-c(rep("Lysine",4),rep("Tryptophan",4),rep("Valine",4))
Source<-(rep(c("Cow","Cat","Monkey","Human"),3))
Values<-c(46846813,3186186135,6816135168,3178168,
          461351,681684351,3584684351,68463513511,
          8463510351,8435186468,1358486,6843513065)

df<-data.frame(Source,amino.acids,Values)

ggplot(df, aes(x=reorder(Source, -Values),y=Values, fill=Source))+
  geom_bar(stat = "identity", color="black")+
  facet_wrap(~amino.acids)+
  scale_y_log10(label = trans_format("log10",math_format(10^.x)))+
  xlab("")+ylab("Transformations Potential (Qu/Ha)")+
  theme(axis.text.x = element_text(angle = 45, hjust=1),
        legend.position = "none")

aminomy.acids您还需要在scale\u y\u log10中设置断点

library(ggplot2)
library(scales)

ggplot(df, aes(x=reorder(Source, -Values),y=Values, fill=Source))+
  geom_bar(stat = "identity", color="black")+
  facet_wrap(~amino.acids)+
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), 
                labels = trans_format("log10", scales::math_format(10^.x)))+
  xlab("")+ylab("Transformations Potential (Qu/Ha)")+
  theme(axis.text.x = element_text(angle = 45, hjust=1),
        legend.position = "none")

我们可以编写一个小的辅助函数
expSup
,它使用
sprintf
,并用上标进行科学记数的数学运算

expSup <- function(w, digits=0) {
  sprintf(paste0("%.", digits, "f*x*10^%d"), w/10^floor(log10(abs(w))), floor(log10(abs(w))))
}


抛出一个警告,但谁在乎呢。

我希望在条顶上有X X 10^X格式的标签,而不是修改比例。太棒了。由于某些原因,它在您第一次运行时失败,但在再次运行时将正常执行。再一次,非常感谢
library(ggplot2)
library(scales)
p <- ggplot(df, aes(x=reorder(Source, -Values), y=Values, fill=Source)) +
  geom_bar(stat="identity", color="black") +
  facet_wrap(~amino.acids) +
  scale_y_log10(label=trans_format("log10",math_format(10^.x))) +
  xlab("") + 
  ylab("Transformations Potential (Qu/Ha)") +
  theme(axis.text.x=element_text(angle=45, hjust=1), legend.position="none") 
p + geom_text(label=parse(text=expSup(Values)), vjust=-.25, size=3)