如何在R中绘制互补累积分布函数(CCDF)(最好在ggplot中)?

如何在R中绘制互补累积分布函数(CCDF)(最好在ggplot中)?,r,ggplot2,cdf,ecdf,R,Ggplot2,Cdf,Ecdf,以下是我的代码和输出(CDF): install.packages(“ggplot2”) 图书馆(GG2) chol您可以使用ggplot\u build提取用于绘图的数据,然后对其进行修改: p <- ggplot(df, aes(x)) + stat_ecdf() pg <- ggplot_build(p)$data[[1]] ggplot(pg, aes(x = x, y = 1-y )) + geom_step() p您也可以一步一步地执行以下操作: # load dat

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

install.packages(“ggplot2”)
图书馆(GG2)

chol您可以使用
ggplot\u build
提取用于绘图的数据,然后对其进行修改:

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

p您也可以一步一步地执行以下操作:

# 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")
#加载数据
胆固醇
# 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")