0

Is it possible to plot the ecdf function in R with bars instead of a line ore steps?

Ore is there another way to plot the cumulative histogram in ggplot with cumulative densities on the y-axis instead of frequencys?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Hark
  • 115
  • 1
  • 11

1 Answers1

1

ggplot has a stat_ecdf() function out of the box, but it doesn't support geom = "bar". We can use stat_bin() in combination with the special variable ..density.. to arrive at a bar plot version of ecdf:

library(ggplot2)
ggplot(df, aes(x)) + stat_ecdf(col = "red") +
                     stat_bin(aes(y = cumsum(..density..)/
                                      sum(..density..)),
                              alpha = 0.8)

enter image description here

mtoto
  • 23,919
  • 4
  • 58
  • 71