Home > Software engineering >  How to calculate 1-CDF in R and plot it?
How to calculate 1-CDF in R and plot it?

Time:04-13

I want to plot the 1-CDF in R

I am using the ggplot stat_ecdf

g1=ggplot()  
  stat_ecdf(data=data_ploting, aes(x, colour=ggg), alpha=0.8, geom= "smooth", pad = FALSE)  
  theme_test ()

CodePudding user response:

The base R function ecdf returns a function that you can use in new expressions, including subtracting it from one. You can therefore also use the usual methods for plotting $1-F(x)$. Example:

x <- sort(iris$Sepal.Length)
cdf <- ecdf(x)
plot(x, 1 - cdf(x), type="s")

CodePudding user response:

The ?stat_ecdf help page shows that there is a "Computed variable" y which holds the calculated CDF value. You can get at that value using stat() and then transform it however you like. So, for example, this should work

ggplot(data_ploting)  
  aes(x = x, y = 1-stat(y), colour=ggg)   
  stat_ecdf(alpha=0.8, geom = "smooth", pad = FALSE)
  • Related