Search code examples
rggplot2plotkolmogorov-smirnov

How to plot two different distributions in one graph in R?


I want to plot two empirical distributions in one Graph to explain the kolmogorov-smirnov Test in my paper.
One ecdf with steps from a sample and one continuous distribution.
So the sample could be:
x = c(12.4,11.8,12.9,12.6,13,12.5,12,11.5,13.2,12.8)
which could be tested for N~(12,1). The result should look something like this plot.

Thanks in advance :)


Solution

  • another minimalist way:

    my_observations = c(12.4,11.8,12.9,12.6,13,12.5,12,11.5,13.2,12.8)
    my_ecdf <- ecdf(my_observations)
    
    curve(my_ecdf(x),
          from = 10, to = 14,
          frame = FALSE ## don't plot border
          )
    
    curve(pnorm(x, mean = 12),
          from = 10, to = 14,
          add = TRUE ## add to existing plot
          )