R 绘制给定累积分布函数的离散密度

R 绘制给定累积分布函数的离散密度,r,ggplot2,data.table,R,Ggplot2,Data.table,我得到了如下形式的离散累积分布函数: set.seed(1) x <- rnorm(100,0,1) y <- ecdf(x)(sort(x)) cdf <- data.table(x=sort(x),y=y) str(cdf) Classes ‘data.table’ and 'data.frame': 100 obs. of 2 variables: $ x: num -2.21 -1.99 -1.8 -1.52 -1.47 ... $ y:

我得到了如下形式的离散累积分布函数:

set.seed(1)
x  <- rnorm(100,0,1)
y  <- ecdf(x)(sort(x))
cdf <- data.table(x=sort(x),y=y)

str(cdf)
   Classes ‘data.table’ and 'data.frame':   100 obs. of  2 variables:
   $ x: num  -2.21 -1.99 -1.8 -1.52 -1.47 ...
   $ y: num  0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 ...
   - attr(*, ".internal.selfref")=<externalptr> 
set.seed(1)

像这样?我不确定你所说的“高度是由累积概率之差给出的”是什么意思,因为对于所有x,它似乎都是0.01

library(dplyr)
cdf %>%
  arrange(x) %>%
  # The "default =" term below lets us assign a leftmost width (and thereby 
  #  display something for 100%) even though lead(x) is NA for the last row.
  mutate(x_next = lead(x, default = max(x) + 0.05),
         y_change = lead(y) - y) %>% 
  ggplot(aes(xmin = x, xmax = x_next,
             ymin = 0, ymax = y)) +
  geom_rect()