2
$\begingroup$

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?

$\endgroup$
  • 4
    $\begingroup$ you could just draw the densities - you don't need simulated values. Try the following and check the various arguments (and examples, where relevant) - ?dnorm, ?curve, ?lines, ?segments, ?abline. $\endgroup$ – Glen_b -Reinstate Monica Nov 13 '13 at 11:32
  • $\begingroup$ Also see ?density and in particular plot(density) $\endgroup$ – Peter Flom - Reinstate Monica Nov 13 '13 at 11:48
  • $\begingroup$ I have been trying to get the exact code for two days but with no succeed. Could you please help me writing exactly the code I need? Thanks in advance $\endgroup$ – Mario Modesto Nov 13 '13 at 11:56
3
$\begingroup$

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)
$\endgroup$
  • $\begingroup$ @MarioModesto, you might consider accepting Flask's answer (green checkmark under the vote arrows). Tks $\endgroup$ – Andre Silva Nov 17 '13 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.