RainyDay1
RainyDay1

Reputation: 23

Draw discrete CDF in R

I want to draw a CDF in R, but I am having some problems. I want it to look like this:

enter image description here

But I get lines between the open and closed points by using the command plot(x,y,type="s") So how do I get rid of those lines?

Upvotes: 2

Views: 2301

Answers (2)

DanY
DanY

Reputation: 6073

This isn't a general purpose example, but it will show you how to build the plot you desire in a couple of steps.

First, let's create some data (notice the zeros at the beginning):

x <- 0:6
fx <- c(0, 0.19, 0.21, 0.4, 0.12, 0.05, 0.03)

Fx <- cumsum(fx)
n <- length(x)

Then let's make an empty plot

plot(x = NA, y = NA, pch = NA, 
     xlim = c(0, max(x)), 
     ylim = c(0, 1),
     xlab = "X label",
     ylab = "Y label",
     main = "Title")

Add closed circles, open circles, and finally the horizontal lines

points(x = x[-n], y = Fx[-1], pch=19)
points(x = x[-1], y = Fx[-1], pch=1)
for(i in 1:(n-1)) points(x=x[i+0:1], y=Fx[c(i,i)+1], type="l")

Viola!

enter image description here


If you insist on not seeing the line "inside" of the white points, do this instead:

points(x = x[-n], y = Fx[-1], pch=19)
for(i in 1:(n-1)) points(x=x[i+0:1], y=Fx[c(i,i)+1], type="l")
points(x = x[-1], y = Fx[-1], pch=19, col="white")
points(x = x[-1], y = Fx[-1], pch=1)

Upvotes: 1

LMc
LMc

Reputation: 18622

You can construct this plot using:

plot(x, y, pch = 16, ylim = c(-0.03, 1.03), ylab = "CDF") # solid points/graphic settings
points(x[-1], y[-length(y)]) # open points
abline(h = c(0, 1), col = "grey", lty = 2) # horizontal lines

enter image description here

Note: plot(x,y, type = "s) does not produce a plot like your original question, but rather a step function with both treads (horizontal lines) and risers (vertical lines):

enter image description here


Data

library(dplyr)

set.seed(1)
df <- data.frame(x = rpois(30, 3)) %>% 
  dplyr::arrange(x) %>% 
  dplyr::add_count(x) %>% 
  dplyr::distinct(x, .keep_all = T) %>% 
  mutate(y = cumsum(n) / sum(n)) 

x <- df$x
y <- df$y

Upvotes: 2

Related Questions