[R] hist function to give each cell equal area

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jan 7 11:47:27 CET 2005


Dan Bolser <dmb at mrc-dunn.cam.ac.uk> writes:

> Hi,
> 
> I want to use hist with non-equi-spaced breaks, picked such that the
> fraction of the data points falling in the cells (defined by 'breaks') is 
> roughly equal accross all cells.
> 
> Is there such a function that will automatically try to determine the
> breaks to fullfill this requirement?
> 
> Something like..
> 
> hist( x, br = magic_function_to_pick_breaks())
> 
> 
> For example, if x looked like this...
> 
> x <- c( 1:5, 10+1:5, 100+1:5 )
> 
> the breaks would define cells like this 
> 
> hist(x,breaks=c(0,5,15,105))
> 
> Is there such a function?

Probably not giving "pretty" numbers, but quantile() gets you most of
the way

 quantile(x,seq(0,1,1/3))
 hist(x,breaks=quantile(x,seq(0,1,1/3)))
 table(cut(x,breaks=quantile(x,seq(0,1,1/3))))

obviously, with that distribution you have to be a little careful:

 hist(x,breaks=quantile(x)) does look somewhat different...

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list