16

Possible Duplicate:
Making a standard normal distribution in R

Using R, draw a standard normal distribution. Label the mean and 3 standard deviations above and below the (10) mean. Include an informative title and labels on the x and y axes.

This is a homework problem. I'm not sure how to get going with the code. How should I get started?

Community
  • 1
  • 1
Stats Rookie
  • 233
  • 1
  • 3
  • 7
  • Alright I made some changes to it, but now how would I go about marking 3 standard deviations above and below the mean? `plot(x,y, type="l", lwd=1,main="Mean of a Standard Normal Distribution",xlab="X",ylab="Y") abline(a=10,b=1,v=10)` – Stats Rookie May 11 '12 at 02:44
  • 1
    `data=rnorm(1e4, mean=10, sd=3)`, then `plot(density(data))` and add normal curve : `curve(dnorm(x, mean=10, sd=3), add=TRUE, col=3)` – Qbik Feb 19 '17 at 16:50

3 Answers3

27

I am pretty sure this is a duplicate. Anyway, have a look at the following piece of code

x <- seq(5, 15, length=1000)
y <- dnorm(x, mean=10, sd=3)
plot(x, y, type="l", lwd=1)

I'm sure you can work the rest out yourself, for the title you might want to look for something called main= and y-axis labels are also up to you.

If you want to see more of the tails of the distribution, why don't you try playing with the seq(5, 15, ) section? Finally, if you want to know more about what dnorm is doing I suggest you look here

nbro
  • 15,395
  • 32
  • 113
  • 196
user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • Alright I made some changes to it, but now how would I go about marking 3 standard deviations above and below the mean? `> plot(x,y, type="l", lwd=1,main="Mean of a Standard Normal Distribution",xlab="X",ylab="Y") > abline(a=10,b=1,v=10)` – Stats Rookie May 11 '12 at 02:32
  • 1
    make `seq(0,20,length=1000)` so you see more of the curve. Now one standard deviation in my code is 3 right? i.e. `sd=3` so three standard deviations above 10 and below should be 1 and 19: `abline(a=1,b=1,v=19)` and `abline(a=1,b=1,v=1)` – user1317221_G May 11 '12 at 07:55
  • data=rnorm(1e4, mean=10, sd=3), then plot(density(data)) and add normal curve : curve(dnorm(x, mean=10, sd=3), add=TRUE, col=3) using `seq` is unnecessary – Qbik Feb 19 '17 at 16:51
7

By the way, instead of generating the x and y coordinates yourself, you can also use the curve() function, which is intended to draw curves corresponding to a function (such as the density of a standard normal function).

see

help(curve)

and its examples.

And if you want to add som text to properly label the mean and standard deviations, you can use the text() function (see also plotmath, for annotations with mathematical symbols) .

see

help(text)
help(plotmath)
Matthieu Dubois
  • 317
  • 1
  • 3
5

Something like this perhaps?

x<-rnorm(100000,mean=10, sd=2)
hist(x,breaks=150,xlim=c(0,20),freq=FALSE)
abline(v=10, lwd=5)
abline(v=c(4,6,8,12,14,16), lwd=3,lty=3)
ECII
  • 10,297
  • 18
  • 80
  • 121