如何使用ggplot2在R中的每个条形图上在geom_bar上放置标签

我发现了,如何使用ggplot2在R中的geom_bar上放置标签,但是它只是将label(numbers)放在仅一个标签上。


假设这是每个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手册以查找一些信息,但是无法使其正常工作。


有什么建议吗?


杨魅力
浏览 4133回答 2
2回答

哆啦的时光机

尝试这个:ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) +&nbsp;&nbsp; &nbsp; &nbsp;geom_bar(position = 'dodge', stat='identity') +&nbsp; &nbsp; &nbsp;geom_text(aes(label=Number), position=position_dodge(width=0.9), vjust=-0.25)

元芳怎么了

要添加到rcs的答案中,如果您希望在x是POSIX.ct日期时将position_dodge()与geom_bar()结合使用,则必须将宽度乘以86400,例如,ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) +&nbsp;&nbsp;geom_bar(position = "dodge", stat = 'identity') +&nbsp;geom_text(aes(label=Number), position=position_dodge(width=0.9*86400), vjust=-0.25)
打开App,查看更多内容
随时随地看视频慕课网APP