直接在ggplot中设置直方图的bin数量 [英] Set number of bins for histogram directly in ggplot

查看:339
本文介绍了直接在ggplot中设置直方图的bin数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的直方图提供geom_histogram箱数,而不是通过binwidth控制箱数. 文档说,我可以通过设置bins参数来做到这一点.但是当我运行

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)

我收到一条包含30个bin的输出消息,好像我根本没有指定binwidth一样.

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

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

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

我尝试用相同的问题将此参数提供给stat_binqplot.我在做错什么吗?

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

我正在使用ggplot2 1.0.1.版.

I'm using ggplot2 version 1.0.1.

推荐答案

只需直接通过bins=x

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

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

产生此结果(警告使用bins = 30使用stat_bin().使用binwidth选择更好的值."):

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

这:

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

产生:

使用ggplot2 2.0.0版

Using ggplot2 version 2.0.0

这篇关于直接在ggplot中设置直方图的bin数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆