rdataframeggplot2plotecdf

Make an ECDF plot of a vector in ggplot


I want to make an ECDF plot in GGplot2. This is a sample of my data (just a vector of numbers with no column name)

c(`6` = 0.152116553279516, `14` = 0.20966399205409, `15` = 0.153878854517702, 
`33` = 0.182997937239927, `34` = 0.157352856182734, `35` = 0.192507071224356, 
`40` = 0.126342215514692, `61` = 0.084224414145397, `62` = 0.0908129423320216, 
`65` = 0.105414066466038, `67` = 0.104768440695237, `69` = 0.121653067192898, 
`70` = 0.0900390641500813, `71` = 0.0902422264702487, `75` = 0.0934569432438857, 
`77` = 0.0932332730883442, `95` = 0.127137531398275, `97` = 0.0890192433829655, 
`107` = 0.111778811576104, `113` = 0.0935501003998418)

I have tried this but it does not work: (note: exp(rfcaret_test1000) is the name of the vector that I provided in the sample data.

x<- as.data.frame(exp(rfcaret_test1000)
ggplot(x, aes(height)) + stat_ecdf(geom = "point")

I see a similar post here and tried the code provided

df <- as.data.frame(exp(rfcaret_test1000)
df <- data.frame(x = c(-Inf, data), 
             xend = c(data, Inf), 
             y = c(0, 0.2, 0.4, 0.4, 0.8, 1))
ggplot(df, aes(x, y)) + 
geom_segment(aes(xend = xend, yend = y), size = 1) +
geom_point(aes(x = xend), shape = 21, fill = "white", size = 3) +
theme_classic()

but I got the following error:

Error in data.frame(x = c(-Inf, data), xend = c(data, Inf), y = 
c(0, 0.2,  : arguments imply differing number of rows: 2663, 6

Solution

  • Assuming that your vector names are meaningful (and maybe your intended x), then you could stack the vector and use this for plotting.

    library(ggplot2)
    rfcaret_test1000 <- c(`6` = 0.152116553279516, `14` = 0.20966399205409, `15` = 0.153878854517702, 
      `33` = 0.182997937239927, `34` = 0.157352856182734, `35` = 0.192507071224356, 
      `40` = 0.126342215514692, `61` = 0.084224414145397, `62` = 0.0908129423320216, 
      `65` = 0.105414066466038, `67` = 0.104768440695237, `69` = 0.121653067192898, 
      `70` = 0.0900390641500813, `71` = 0.0902422264702487, `75` = 0.0934569432438857, 
      `77` = 0.0932332730883442, `95` = 0.127137531398275, `97` = 0.0890192433829655, 
      `107` = 0.111778811576104, `113` = 0.0935501003998418)
    
    ggplot(stack(rfcaret_test1000), aes(as.integer(ind), values)) + 
      stat_ecdf(geom = "point")
    

    Created on 2023-03-02 with reprex v2.0.2