Reputation: 491
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()
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
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")
Upvotes: 1
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()
Upvotes: 4