如何把标签上geom_bar在R各自酒吧GGPLOT2(How to put labels over

2019-06-17 18:40发布

我发现这一点, 如何把标签上geom_bar R中与GGPLOT2 ,但它只是把标签(编号)在只有一个酒吧。

这里,让我们说,两间酒吧每个x轴,如何做同样的事情?

我的数据和代码如下所示:

dat <- read.table(text = "sample Types Number
sample1 A   3641
sample2 A   3119
sample1 B   15815
sample2 B   12334
sample1 C   2706
sample2 C   3147", header=TRUE)

library(ggplot2)
bar <- ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + 
  geom_bar(position = 'dodge') + geom_text(aes(label=Number))

然后,我们会得到:

看来,一些案文也被定位在“躲闪”的格局。 我搜索geom_text手动找到一些信息,但不能使它工作。

建议?

Answer 1:

试试这个:

ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + 
     geom_bar(position = 'dodge', stat='identity') +
     geom_text(aes(label=Number), position=position_dodge(width=0.9), vjust=-0.25)



Answer 2:

要添加到RCS的回答,如果你想使用position_dodge()与geom_bar()时,x是POSIX.ct日期,则必须由86400乘以宽度,例如,

ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + 
 geom_bar(position = "dodge", stat = 'identity') +
 geom_text(aes(label=Number), position=position_dodge(width=0.9*86400), vjust=-0.25)


文章来源: How to put labels over geom_bar for each bar in R with ggplot2