Search code examples
rcurvenormal-distribution

Overlay three curves in the same y axis


I want to overlay three curves of the normal distribution with three different sigma values. I have done this:

curve(dnorm(x, mean(x), sd(x)), col = 2, lwd = 2)
par(new = TRUE)
curve(dnorm(x, mean(x), sd(x)/2), col = 3, lwd = 2)
par(new = TRUE)
curve(dnorm(x, mean(x), sd(x)*2), col = 4, lwd = 2)

And the result isenter image description here But I want to use only one y axis scale, so the three plots are one under the other and not on the same y scale. Also I want to expand the range of the x axis, to be from -3 to 3 for example


Solution

  • Open a blank graphics device first by setting plot parameter type = "n". Then add = TRUE the 3 curves.

    plot(c(0, 1), c(0, 3), type = "n", xlab = "", ylab = "")
    curve(dnorm(x, mean(x), sd(x)), col = 2, lwd = 2, add = TRUE)
    curve(dnorm(x, mean(x), sd(x)/2), col = 3, lwd = 2, add = TRUE)
    curve(dnorm(x, mean(x), sd(x)*2), col = 4, lwd = 2, add = TRUE)
    

    enter image description here