0

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 ()
  • 1
    What exactly is the "1-CDF"? Where is this defined? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Maybe you want `aes(x, y=1-stat(y), colour=ggg)`? – MrFlick Apr 12 '22 at 06:53
  • What I mean is to compute the 1-F(x) where F(x) is the empirical CDF P(X<=x) , and then plot it – Jinane Jouni Apr 12 '22 at 12:09
  • Thanks for the explanation that you seek $1-F(x)$. I have edited my answer to show how this is done. Do not forget to mark the answer as accepted, if it actually solves your problem, so that the question is marked as answered. – cdalitz Apr 12 '22 at 12:44

2 Answers2

2

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")
cdalitz
  • 1,019
  • 1
  • 6
  • 7
0

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)
MrFlick
  • 195,160
  • 17
  • 277
  • 295