【问题标题】:How to add frequency count labels to the bars in a bar graph using ggplot2? [duplicate]如何使用ggplot2向条形图中的条形添加频率计数标签? [复制]
【发布时间】:2014-10-24 17:55:02
【问题描述】:

我想将 [r] 因子变量的频率分布绘制为条形图,其中条形表示因子水平的频率计数。我用 ggplot2 来做,没有问题。

我不知道如何将频率计数标签添加到条形图中的条形。我试过的语法如下:

ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))

我想我在 stackoverflow 和 W.Chang 的“R Graphics Cookbook”中进行了彻底的搜索,但在上面的 geom_text() 美学中我找不到与“y”匹配的参数的任何具体答案。我尝试了一些变体,例如: (y = ..count..) 但它不起作用。

我将不胜感激。谢谢...

【问题讨论】:

  • 从这个问题的一些部分注释中,给出的例子是 geom_text(aes(label = numbers), vjust=-1, position = position_dodge(0.9), size = 3) # try numbers跨度>

标签: r ggplot2 geom-bar geom-text


【解决方案1】:
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)

【讨论】:

  • 对我来说,它适用于stat='count'
  • Stat = "count" 也为我工作。错误说StatBin 需要一个连续变量,但我有分类变量。
  • 我收到此警告:stat_bin() 使用 bins = 30。使用binwidth 选择更好的价值。
  • 我收到一条错误消息,提示“stat_count 需要以下缺失的美学:x”
  • ggplot2 3.3.0 版现在支持geom_text(stat = "count", aes(label = after_stat(count)), vjust = -1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
相关资源
最近更新 更多