为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

杨魅力 2019-10-26 12:21:11
我发现了,如何使用ggplot2在R中的geom_bar上放置标签,但是它只是将label(numbers)放在仅一个标签上。假设这是每个x轴的两个条形,该怎么做?我的数据和代码如下所示:dat <- read.table(text = "sample Types Numbersample1 A   3641sample2 A   3119sample1 B   15815sample2 B   12334sample1 C   2706sample2 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手册以查找一些信息,但是无法使其正常工作。有什么建议吗?
查看完整描述

2 回答

?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

尝试这个:


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)


查看完整回答
反对 回复 2019-10-26
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

要添加到rcs的答案中,如果您希望在x是POSIX.ct日期时将position_dodge()与geom_bar()结合使用,则必须将宽度乘以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)


查看完整回答
反对 回复 2019-10-26
  • 2 回答
  • 0 关注
  • 4126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信