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"]]))
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With