Set number of bins for histogram directly in ggplot

只愿长相守 提交于 2019-12-04 10:42:11

问题


I'd like to feed geom_histogram the number of bins for my histogram instead of controlling bins through binwidth. The documentation says I can do this by setting the bins argument. But when I run

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

I get an output message with 30 bins, as if I didn't specify binwidth at all.

stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

I've tried feeding this argument to stat_bin and qplot with the same problem. Am I doing something wrong?

I'm using ggplot2 version 1.0.1.


回答1:


Just pass bins=x directly

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

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

Produces this (with warning "stat_bin() using bins = 30. Pick better value with binwidth."):

And this:

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

Produces:

Using ggplot2 version 2.0.0



来源:https://stackoverflow.com/questions/34774120/set-number-of-bins-for-histogram-directly-in-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!