我想在每个栏的顶部添加数据标签。但是我希望它不是+ e而是10 ^格式。有关轴的文献很多。标签不适用。我的代码如下:
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")
添加geom_text(label = trans_format("log10",math_format(10^.x)))
失败。
您还需要在scale_y_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))))
}
您的情节,
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)
发出警告,但谁在乎。