Search code examples
rggplot2ecdf

how to make a cdf plot smoother and label y axis


I read parameters "data1" and "data2" from files and use this code to plot cdf but I have two problems:

  1. make the figure smoother
  2. label Y axis to CDF

Please notice that this code is correct but I need some modifications.

df <- data.frame(x = c(data1, data2), ggg=factor(rep(1:2, c(19365,19365))))

ggplot(df, aes(x, colour = ggg)) + 
  stat_ecdf() + 
  labs(x='Time (ms)', ggg='CDF', fill='') + 
  theme_bw()+
  theme(panel.grid.major = element_line(colour = 'grey'),
        panel.border = element_rect(colour = 'black'),
        axis.line = element_blank(),
        panel.background = element_blank(),
        legend.direction='vertical',
        legend.position = c(1, 0.5),
        legend.justification = c(1, 0.5),
        legend.background = element_rect(colour = NA)) +
  scale_colour_hue(name='', labels=c('IEEE 802.11p','Our protocol'))

Solution

  • The empirical distribution function is always a step function and you should not smooth it in any way. Having said that, you can get the values for the empirical distribution function using ecdf. If you want to do any smoothing on the result (and this is not suggested), you can.

    require(dplyr)
    res <- df %>% 
      group_by(ggg) %>%
      do(data.frame(x = sort(.$x), 
                    ecdf = ecdf(.$x)(sort(.$x))))
    ggplot(res, aes(x, ecdf, colour = ggg)) + geom_step()
    

    To relabel the y axis, you can use

    labs(x='Time (ms)', y='CDF')