How to use the function curve in [R] to graph a normal curve?

柔情痞子 提交于 2019-11-28 07:25:20

问题


I'm trying to make a histogram in [R], and the 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 plot the normal curve by curve function shows me 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?


回答1:


You just need to drop the "w" argument 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 the "unit Normal" you supply "mean" and "sd" arguments (and do remember to change the plot limits for both 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")




回答2:


Simple...

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


来源:https://stackoverflow.com/questions/9046664/how-to-use-the-function-curve-in-r-to-graph-a-normal-curve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!