【问题标题】:Set number of bins for histogram directly in ggplot直接在ggplot中设置直方图的bin数
【发布时间】:2016-04-18 20:47:00
【问题描述】:

我想向geom_histogram 提供直方图的 bin 数量,而不是通过 binwidth 控制 bin。 The documentation 说我可以通过设置 bins 参数来做到这一点。但是当我运行时

ggplot(data = iris, aes(x = Sepal.Length)) + stat_bin(bins = 5)

我得到一个包含 30 个 bin 的输出消息,好像我根本没有指定 binwidth。

stat_bin:binwidth 默认为 range/30。使用 'binwidth = x' 进行调整。

我尝试将此论点提供给 stat_binqplot,但遇到了同样的问题。我做错了吗?

我使用的是 ggplot2 版本 1.0.1。

【问题讨论】:

  • 你能分享你的数据吗
  • 你用的是什么版本的ggplot?
  • 如果你更新到 ggplot 2.0,stat_bin(bins = x) 命令应该可以工作。
  • 为我工作。考虑更新 ggplot 和/或重新启动您的 r 会话。
  • 它对我来说很好,但奇怪的是:它总是产生比我指定的多一个 bin。一个 index-0 错误? (ggplot 2.0.0)

标签: r ggplot2 histogram


【解决方案1】:

直接传bins=x就行了

library(ggplot2)
df <- data.frame(a = rnorm(10000))

ggplot(df, aes(x=a)) + geom_histogram()

产生这个(带有警告“stat_bin() using bins = 30. Pick better value with binwidth.”):

还有这个:

ggplot(df, aes(x=a)) + geom_histogram(bins=10)

生产:

使用 ggplot2 2.0.0 版

【讨论】:

  • @bouncyball 的帽子提示,显然你在我写这篇文章时评论了同样的效果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多