2016-01-13 124 views
8

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

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

我得到一个带有30个bin的输出消息,就像我没有指定binwidth一样。

stat_bin:binwidth默认为范围/ 30。使用'binwidth = x'来调整它。

我试过把这个参数提供给stat_binqplot带有同样的问题。难道我做错了什么?

我正在使用ggplot2版本1.0.1。

+3

你可以分享你的资料,请 – mtoto

+3

您正在使用什么版本ggplot的? – Heroka

+0

@mtoto我改变了问题,所以它使用通用数据 – Empiromancer

回答

12

只是通过bins=x直接

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

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

产生以下(含警告 “使用bins = 30stat_bin()binwidth挑选更好的价值。”):

enter image description here

而且这样的:

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

产地:

enter image description here

使用GGPLOT2版本2.0.0

+0

帽子提示@bouncyball,显然你在我写这篇文章时评论的效果相同 – arvi1000