[R] Histogram

Rui Barradas ruipbarradas at sapo.pt
Thu Jul 5 22:01:52 CEST 2012


Hello,

With the confusion between bin size and width the OP started, I'll 
repost my answer with a final line. Sorry for the repetition.


h <- hist(x, breaks=quantile(x, probs=seq(0, 1, by=1/20)))
h$counts
[1] 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50


Hope this helps,

Rui Barradas

Em 05-07-2012 20:47, Sarah Goslee escreveu:
> There's no reason you can't do that with normally-distributed data,
> though I'm not sure why you'd want to. My point was rather that you
> can't specify the bin width and size both. If you let the bin size
> vary, this will work:
>
> set.seed(1234)
> mydata <- rnorm(1000, mean = 2, sd = 4)
> mydata.hist <- hist(mydata, breaks=quantile(mydata, probs=seq(0, 1,
> length.out = length(mydata)/50 + 1)))
> mydata.hist$counts
>
>
> Sarah
>
> On Thu, Jul 5, 2012 at 3:37 PM, Jim Silverton <jim.silverton at gmail.com> wrote:
>> Thanks Sarah!!
>> Ok so if I have say x = runif(1000,0,1) say instead if the normal and I want
>> a histogram with bins that have an equal number of observations. For example
>> if I want each bin to have 50 observations, how do I do this?
>>
>>
>>
>> On Thu, Jul 5, 2012 at 3:34 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>>>
>>> Hi Jim,
>>>
>>> You can't specify both number of bins and bin size. You can specify
>>> breaks: either the number of bins or the location of breakpoints. A
>>> histogram with 20 bins of 50 observations each must by definition come
>>> from a uniform distribution.
>>>
>>> What are you trying to accomplish?
>>>
>>> Sarah
>>>
>>> On Thu, Jul 5, 2012 at 3:29 PM, Jim Silverton <jim.silverton at gmail.com>
>>> wrote:
>>>> I have a column of 1000 datapoints from the normal distribution with
>>>> mean 2
>>>> and variance 4. How can I get a histogram of these observations with 20
>>>> bins with each bin having 50 observations?
>>>>
>>>> --
>>>> Thanks,
>>>> Jim.
>>>
>



More information about the R-help mailing list