Home > Net >  How to plot two different distributions in one graph in R?
How to plot two different distributions in one graph in R?

Time:05-26

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 :)

CodePudding user response:

Maybe you want something like this:

library(stats)

x1 = c(12.4,11.8,12.9,12.6,13,12.5,12,11.5,13.2,12.8)
x2 = rnorm(12, 12, 1)

plot(ecdf(x1),pch = 1, xlim=c(10,15), main=NULL)
par(new=TRUE)        
plot(ecdf(x2),pch = 2, xlim=c(10,15), main=NULL)

Output:

enter image description here

CodePudding user response:

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
      )
  • Related