2

I want to plot a histogram with density line. The code below does not work. Any idea why?

control <- c(4.17,3.05,5.18,4.01,6.11,4.1,5.17,3.57,5.33,5.59,
         4.66,5.58,3.66,4.50,3.90,4.61,5.62,4.53,6.05,5.14)  
hist(control,col="gray",xlab="Utility",prob=TRUE,
main="Histogram of individual utilities")
curve(dnorm(control,mean=mean(control),
sd=sd(control)), add=TRUE,col="red")
Günal
  • 751
  • 1
  • 14
  • 29
  • possible duplicate of [Fitting a density curve to a histogram in R](http://stackoverflow.com/questions/1497539/fitting-a-density-curve-to-a-histogram-in-r) – Henrik Nov 14 '14 at 13:36

1 Answers1

3

What was your error message? You should have told us. I get:

Error in curve(dnorm(control, mean = mean(control), sd = sd(control)),  : 
  'expr' must be a function, or a call or an expression containing 'x'

So make it an expression containing x:

curve(dnorm(x,mean=mean(control),sd=sd(control)), add=TRUE,col="red")

hist plus density

Spacedman
  • 92,590
  • 12
  • 140
  • 224