Übel Yildmar
Übel Yildmar

Reputation: 491

How to plot a Complementary Cumulative Distribution Function (CCDF) in R (preferbly in ggplot)?

Here is my code and my output (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()

enter image description here

I'd like to plot a CCDF function, what is the "inverse" of the CDF function: CCDF(x)=1-CDF(x). I cannot find any sources about this problem. Is there any easy way?

Upvotes: 3

Views: 4639

Answers (2)

rafa.pereira
rafa.pereira

Reputation: 13807

In a step-by-step, you can also do this:

# 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")

enter image description here

Upvotes: 1

lbusett
lbusett

Reputation: 5932

You can use ggplot_build to extract the data used for the plot and then modify it:

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

enter image description here

Upvotes: 4

Related Questions