【问题标题】:How to print Frequencies on top of Histogram bars in ggplot如何在ggplot的直方图条上打印频率
【发布时间】:2021-08-27 18:12:01
【问题描述】:

我想知道是否可以在 ggplot 直方图中显示每个计数条顶部的频率。

这是我目前得到的代码:

br <- seq(0, 178, 10)
ggplot(dfAllCounts, aes(x=months)) + geom_histogram(aes(months), bins = 30, fill="#d2aa47", color = '#163B8B', size = .8, alpha = 0.3) + 
  scale_x_continuous(breaks = br)

我想在上面显示那个月数,谢谢

【问题讨论】:

    标签: r ggplot2 r-markdown histogram knitr


    【解决方案1】:

    切换到底层的 stat_bin 函数,而不是 geom_histogram 包装器,您可以在其中使用内置的 geom="text",结合 after_stat(count) 将标签添加到直方图。

    ggplot(mpg,aes(x=displ)) + 
      stat_bin(binwidth=1) +
      stat_bin(binwidth=1, geom="text", aes(label=after_stat(count)), vjust=0) 
    

    修改自https://stackoverflow.com/a/24199013/10276092

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 2018-05-26
      • 2013-10-02
      • 1970-01-01
      • 2013-10-25
      • 2019-03-18
      • 1970-01-01
      相关资源
      最近更新 更多