2

I have data cdecn:

set.seed(0)
cdecn <- sample(1:10,570,replace=TRUE)
a <- rnorm(cdecn,mean(cdecn),sd(cdecn))

I have created a plot which displays the cumulative probabilities.

aprob <- ecdf(a)
plot(aprob)

enter image description here

I am wondering how I can switch the x-axis and y-axis to get a new plot, i.e., the inverse of ECDF.

Also, for the new plot, is there a way to add a vertical line through where the my curve intersects 0?

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Dylan
  • 37
  • 3

3 Answers3

2

We can do the following. My comments along the code is very explanatory.

## reproducible example
set.seed(0)
cdecn <- sample(1:10,570,replace=TRUE)
a <- rnorm(cdecn,mean(cdecn),sd(cdecn))  ## random samples

a <- sort(a)  ## sort samples in ascending order
e_cdf <- ecdf(a)  ## ecdf function
e_cdf_val <- 1:length(a) / length(a)  ## the same as: e_cdf_val <- e_cdf(a)

par(mfrow = c(1,2))

## ordinary ecdf plot
plot(a, e_cdf_val, type = "s", xlab = "ordered samples", ylab = "ECDF",
     main = "ECDF")

## switch axises to get 'inverse' ECDF
plot(e_cdf_val, a, type = "s", xlab = "ECDF", ylab = "ordered sample",
     main = "'inverse' ECDF")

## where the curve intersects 0
p <- e_cdf(0)
## [1] 0.01578947

## highlight the intersection point
points(p, 0, pch = 20, col = "red")

## add a dotted red vertical line through intersection
abline(v = p, lty = 3, col = "red")

## display value p to the right of the intersection point
## round up to 4 digits
text(p, 0, pos = 4, labels = round(p, 4), col = "red")

enter image description here

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • No, no need to remove your answer! I just wonder if you have a better method of adding the straight line. I finally found something, but it's probably not very direct. – Hack-R Jul 23 '16 at 16:02
  • The plot with the switched axis is what I was looking for. I will be adding a second curve to the plot and I want to illustrate the differences between the curves. Therefore I require a vertical line through the point (x,0). In this case I would want a vertical line at roughly x=0.05. So my question is how to identify where this point is. – Dylan Jul 23 '16 at 16:08
2
cdecn <- sample(1:10,570,replace=TRUE)
a <- rnorm(cdecn,mean(cdecn),sd(cdecn))

aprob <- ecdf(a)
plot(aprob)

# Switch the x and y axes
x <- seq(0,1,0.001754386)
plot(y=knots(aprob), x=x, ylab = "Fn(y)")

enter image description here

# Add a 45 degree straight line at 0, 0
my_line <- function(x,y,...){
  points(x,y,...)
  segments(min(x), y==0, max(x), max(y),...)
}
lines(my_line(x=x, y = knots(aprob)))

enter image description here

Hack-R
  • 22,422
  • 14
  • 75
  • 131
0

The "straight line at x==0" bit makes me suspect that you want a QQplot:

qqnorm(a)
qqline(a)
lgautier
  • 11,363
  • 29
  • 42