首页 > 解决方案 > R更改geom_bar()中文本的位置

问题描述

我想用geom_text(). 每个条形图上方应该是变量的计数。但在这种情况下,文本的位置在条形图的中间。有人可以帮我解决这个问题吗?

new.data <- diamonds[ which( diamonds$clarity =="VS1" | diamonds$clarity =="VS2") , ]

ggplot(data=new.data, aes(x=clarity, fill=cut)) +
  geom_bar(position = "dodge",stat = "count") +
  geom_text(stat='count', aes(label=..count..), vjust=-1)

在此处输入图像描述

标签: rggplot2

解决方案


在此处添加position类似问题中提到的论点

new.data <- diamonds[ which( diamonds$clarity =="VS1" | diamonds$clarity =="VS2") , ]

ggplot(data=new.data, aes(x=clarity, fill=cut)) +
  geom_bar(position = "dodge",stat = "count") +
  geom_text(stat='count', aes(label=..count..), vjust=-1,
            position = position_dodge(width = 0.9))

输出:

阴谋

就像 TobKel 在评论中提到的那样,可以随宽度变化。


推荐阅读