How to use a function curve in [R] to plot a curve?

I am trying to make a histogram in [R] and a normal curve that describes the histogram as follows:

w<-rnorm(1000) hist(w,col="red",freq=F,xlim=c(-5,5)) curve(dnorm(w),-5,5,add=T,col="blue") 

But when I try to build a normal curve, I see the following error:

 Error en curve(dnorm(w), -5, 5, add = T, col = "blue") : 'expr' must be a function, or a call or an expression containing 'x' 

What am I doing wrong?

+7
source share
2 answers

You just need to drop the argument "w" to dnorm in curve :

 w<-rnorm(1000) hist(w,col="red",freq=F,xlim=c(-5,5)) curve(dnorm,-5,5,add=T,col="blue") 

To use something other than "unit Normal", you specify the arguments "mean" and "sd" (and remember to change the graph limits for hist and curve :

 w<-rnorm(1000, mean=10, sd=2) hist(w, col="red", freq=F, xlim=10+c(-5,5)) curve( dnorm(x, mean=10,sd=2), 5, 15, add=T, col="blue") 

enter image description here

+10
source

Plain...

 curve(dnorm(w, mean=mean(w), sd=sd(w)), y = 5, to = 15, add=T, col="blue") 
+1
source

All Articles