R – Cumulative histogram with ggplot2

ggplot2r

How could I get a cumulative histogram like this

x <- runif(100,0,10)
h <- hist(x)
h[["counts"]] <- cumsum(h[["counts"]])
plot(h)

with ggplot2?

I want also to draw a polygon like this

lines(h[["breaks"]],c(0,h[["counts"]]))

Best Answer

To make cumulative histogram use geom_histogram() and then use cumsum(..count..) for y values. Cumulative line can be added with stat_bin() and geom="line" and y values calculated as cumsum(..count..).

ggplot(NULL,aes(x))+geom_histogram(aes(y=cumsum(..count..)))+
       stat_bin(aes(y=cumsum(..count..)),geom="line",color="green")

enter image description here

Related Topic