【问题标题】:How to plot a Complementary Cumulative Distribution Function (CCDF) in R (preferbly in ggplot)?如何在 R 中绘制互补累积分布函数(CCDF)(最好在 ggplot 中)?
【发布时间】:2017-03-04 18:38:32
【问题描述】:

这是我的代码和输出 (CDF):

install.packages("ggplot2")
library(ggplot2)


chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
df <- data.frame(x = chol$AGE)
ggplot(df, aes(x)) + stat_ecdf()

我想绘制一个 CCDF 函数,CDF 函数的“逆”是什么:CCDF(x)=1-CDF(x)。我找不到有关此问题的任何来源。有什么简单的方法吗?

【问题讨论】:

    标签: r ggplot2 cdf ecdf


    【解决方案1】:

    您可以使用ggplot_build提取用于绘图的数据,然后对其进行修改:

    p  <- ggplot(df, aes(x)) + stat_ecdf()
    pg <- ggplot_build(p)$data[[1]]
    ggplot(pg, aes(x = x, y = 1-y )) + geom_step()
    

    【讨论】:

      【解决方案2】:

      一步一步,你也可以这样做:

      # load data
        chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
      
      
      # get the ecdf - Empirical Cumulative Distribution Function of v
        my_ecdf <- ecdf( chol$AGE )
      
      # now put the ecdf and its complementary in a data.frame
        df <- data.frame( x = sort(chol$AGE),
                          y = 1-my_ecdf(sort(chol$AGE) ))
      
      # plot
        ggplot(data=df, aes(x, y) ) +
          geom_line() +
          geom_point(color="red")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-11
        • 2011-04-02
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 2014-11-20
        相关资源
        最近更新 更多