【问题标题】:Plotting CDF in histogram form in R在 R 中以直方图形式绘制 CDF
【发布时间】:2020-03-27 19:12:19
【问题描述】:

我不是统计专家,但我一直试图从一组点中绘制一个 cdf。我已经尝试过 R 和 Python。这些是我的示例点集:(1.5,1.5,2.5,3.5,3.5,3.5,4.5,5.5,5.5,6)

使用 R 中的 ecdf 函数,我设法得到了这个:

这是我的代码:

data <- c(1.5,1.5,2.5,3.5,3.5,3.5,4.5,5.5,5.5,6)
plot(ecdf(data))

有没有办法获得与直方图相同的绘图,或者这从根本上是错误的?

【问题讨论】:

    标签: r histogram cdf


    【解决方案1】:

    这是你的意思吗?

    par(mar = c(5,5,2,5))
    data <- c(1.5,1.5,2.5,3.5,3.5,3.5,4.5,5.5,5.5,6)
    h <- hist(
      data,
      breaks = seq(0, 10, 1),
      xlim = c(0,10))
    
    par(new = T)
    
    ec <- ecdf(data)
    plot(x = h$mids, y=ec(h$mids)*max(h$counts), col = rgb(0,0,0,alpha=0), axes=F, xlab=NA, ylab=NA)
    lines(x = h$mids, y=ec(h$mids)*max(h$counts), col ='red')
    axis(4, at=seq(from = 0, to = max(h$counts), length.out = 11), labels=seq(0, 1, 0.1), col = 'red', col.axis = 'red')
    mtext(side = 4, line = 3, 'Cumulative Density', col = 'red')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 2020-02-06
      • 2012-09-13
      • 1970-01-01
      • 2014-03-07
      • 2015-07-12
      • 1970-01-01
      • 2016-11-23
      相关资源
      最近更新 更多