【问题标题】:cumulative probability plot from frequency table频率表中的累积概率图
【发布时间】:2012-09-12 18:59:59
【问题描述】:

有没有办法从频率表中绘制累积概率?我的意思是它的“平滑”版本,类似于 geom_density() 绘图的方式。

到目前为止,我设法将单独计算的概率绘制为由线连接的点,但看起来不太好。

【问题讨论】:

  • 您应该提供示例数据。
  • 另外,你应该看看geom_smooth

标签: r ggplot2


【解决方案1】:

我生成了一些测试数据:

set.seed(1)
x <- sort(sample(1:100, 20))
p <- runif(x); p <- cumsum(p)/sum(p)
table <- data.frame(x=x, prob=p)

您可以使用 ggplot2 包中的 geom_smooth。

require("ggplot2")
qplot(x=x, y=p, data=table, aes(ymin=0, ymax=1)) + ylab("ecf") + 
geom_smooth(se=F, stat="smooth", method="loess", fullrange=T, fill="lightgrey", size=1)

作为替代方案,一种通过参数指定平滑的简单方法尝试 decon 包中的 DeconCdf:

require("decon")
plot(DeconCdf(x, sig=1))

如果你想使用 ggplot,你首先必须在 data.frame 中转换 Decon 函数对象。

f <- DeconCdf(x, sig=1)
m <- ggplot(data=data.frame(x=f$x, p=f$y), aes(x=x, y=p, ymin=0, ymax=1)) + ylab("ecf")
m + geom_line(size=1)

使用 sig 参数作为平滑参数:

f <- DeconCdf(x, sig=0.3)
m <- ggplot(data=data.frame(x=f$x, p=f$y), aes(x=x, y=p, ymin=0, ymax=1)) + ylab("ecf")
m + geom_line(size=1)

【讨论】:

    【解决方案2】:

    此版本使用来自geom_density 的平滑线绘制直方图:

    # Generate some data:
    set.seed(28986)
    x2 <- rweibull(100, 1, 1/2)
    
    # Plot the points:
    library(ggplot2)
    library(scales)
    ggplot(data.frame(x=x2),aes(x=x, y=1-cumsum(..count..)/sum(..count..))) +
      geom_histogram(aes(fill=..count..)) +
      geom_density(fill=NA, color="black", adjust=1/2) +
      scale_y_continuous("Percent of units\n(equal to or larger than x)",labels=percent) +
      theme_grey(base_size=18)
    

    请注意,由于个人偏好,我使用了 1 -“累积概率”(我认为它看起来更好,而且我习惯于处理“reliability”指标),但显然这只是一个偏好,您可以通过删除忽略aes 中的 1- 部分。

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      相关资源
      最近更新 更多