gpt4 book ai didi

r - 直接在 ggplot 中设置直方图的 bin 数量

转载 作者:行者123 更新时间:2023-12-02 18:08:08 25 4
gpt4 key购买 nike

我想向 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 defaulted to range/30. Use 'binwidth = x' to adjust this.

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

我使用的是 ggplot2 版本 1.0.1。

最佳答案

直接传递bins=x即可

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

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

产生这个(带有警告“stat_bin() using bins = 30。使用binwidth选择更好的值。”):

enter image description here

还有这个:

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

产品:

enter image description here

使用ggplot2版本2.0.0

关于r - 直接在 ggplot 中设置直方图的 bin 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com