【问题标题】:How to show count of each bin on histogram on the plot [duplicate]如何在绘图的直方图上显示每个 bin 的计数 [重复]
【发布时间】:2014-05-20 19:42:32
【问题描述】:

我想出了如何从 ggplot 中获取每个 bin 的计数,有人知道如何在绘图上显示这些数字吗?

g <- ggplot()+geom_histogram()
ggplot_build(g)$data[[1]]$count

【问题讨论】:

标签: r ggplot2 histogram


【解决方案1】:

您可以添加stat_bin 来为您计算计数,然后将这些值用于标签和高度。这是一个例子

set.seed(15)
dd<-data.frame(x=rnorm(100,5,2))
ggplot(dd, aes(x=x))+ geom_histogram() +
    stat_bin(aes(y=..count.., label=..count..), geom="text", vjust=-.5) 

【讨论】:

  • 如果您只想显示非零值怎么办?
  • 快速删除零的脏方法:aes(y=..count.., label=ifelse(..count..==0,"",..count..)
猜你喜欢
  • 2015-07-15
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多