Solved – Plot 3 density plots in the same graph, showing also standard deviation limits in each one

data visualizationdensity functionggplot2kernel-smoothingnormal distribution

I have obtained from a paper next results:

Variable 1: Mean=165, sd=15

Variable 2: Mean=149, sd=18

Variable 3: Mean=134, sd=25

I have simulated normal distributions using rnorm.
This is the way (with a n=1000): rnorm(1000,165,15). The same procedure with the others.

So now I want a common graph that shows the 3 normal distributions (density plots) at the same time.
And also, that each of the density plots shows standard deviations bounds (1 and 2 sigmas) and if possible, means. This may be represented as vertical lines from the X axis til the curve of the plot.

Anybody knows which is the exact code in R?

Best Answer

enter image description here

x<-0:300

a<-dnorm(x,165,15)
b<-dnorm(x,149,18)
c<-dnorm(x,134,25)

plot(x,a, type="l", lwd=3, ylim=c(0,1.2*max(a,b,c)), ylab="Probability Density")
segments(150,0, 150, a[which(x==150)], lwd=3, lty=2)
segments(180,0, 180, a[which(x==180)], lwd=3, lty=2)
text(165, a[which(x==165)], "165", pos=3)

lines(x,b, type="l", lwd=3, col="Red")
segments(131,0, 131, b[which(x==131)], lwd=3, lty=2, col="Red")
segments(167,0, 167, b[which(x==167)], lwd=3, lty=2, col="Red")
text(149, b[which(x==149)], "149", col="Red", pos=3)

lines(x,c, type="l", lwd=3, col="Blue")
segments(109,0, 109, c[which(x==109)], lwd=3, lty=2, col="Blue")
segments(159,0, 159, c[which(x==159)], lwd=3, lty=2, col="Blue")
text(134, b[which(x==134)], "134", col="Blue", pos=3)
Related Question