[R] how to draw cumulative histogram

francoisromain@free.fr francoisromain at free.fr
Tue Nov 8 15:56:40 CET 2005


Selon Lisa Wang <lisawang at uhnres.utoronto.ca>:

> Hello there,
>
> I am using R to plot some cumulative histogram for my data. Please help
> in this case.
>
> Thank you
>
> Lisa Wang

Hi Lisa,

Here is one way (if i am right in what is a cumulative histogram), using the
existing possibilities

cumhist <- function(x, plot=TRUE, ...){
  h <- hist(x, plot=FALSE, ...)
  h$counts <- cumsum(h$counts)
  h$density <- cumsum(h$density)
  h$itensities <- cumsum(h$itensities)

  if(plot)
    plot(h)
  h
}


R> x <- rnorm(100)
R> cumhist(x)
R> # hist(x, add=TRUE, col="cyan")
R> # if you want to overlay the original histogram

-- Romain




More information about the R-help mailing list