简体   繁体   中英

Is there a way to display plot geom_text data labels in scientific notation in R with ggplot2?

I would like to display plot labels in scientific notation. It seems like this might be possible by wrapping the variable ('TotVol') in a function, however I have not been able to find any suggestions as to how to do this using the ggplot notation for plots.

The call for my plot is as follows

p<-ggplot(data=df, aes(x=DayType, y=TotVol, fill = Year))+
  geom_bar(stat="identity", position=position_dodge())+
  geom_text(aes(label=TotVol),position=position_dodge(width=0.9), vjust = -0.25)

在此处输入图像描述

If I understand correctly you want that the label on top of the bar is displayed using scientific notation. One possibility to do that is adjusting the formatting with formatC :

library(ggplot2)                 
ggplot(data=df, aes(x=DayType, y=TotVol, fill = Year))+
  geom_bar(stat="identity", position=position_dodge())+
  geom_text(aes(label=formatC(TotVol, format = "e")),
            position=position_dodge(width=0.9), vjust = -0.25)

在此处输入图像描述

Data

df <- data.frame(DayType = c("a", "b", "a", "b"), TotVol=rep(200456567, 4), Year=c("1995", "1995", "2000", "2000") )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM