【问题标题】:R Change position of text in geom_bar() [duplicate]R更改geom_bar()中文本的位置[重复]
【发布时间】:2019-10-22 21:11:38
【问题描述】:

我想用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)

【问题讨论】:

标签: r ggplot2


【解决方案1】:

添加类似问题here中提到的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在评论中提到的那样,可以随宽度变化。

【讨论】:

  • 是的,这行得通。但是使用 position_dodge(width = 0.9) 看起来更好
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 2013-03-03
  • 2018-07-10
  • 2014-07-31
相关资源
最近更新 更多