0

I'm trying to create a density plot and add it a normal curve.

histvb <- hist(color$VB,
               probability=TRUE,
               breaks=seq(4000, 16000, by=1000),
               main=NULL,xlab=c("Visible Brigthness"), 
               ylab=("Probability Densities"),cex.lab=1.2) 
vb<-color$VB
xfit<-seq(min(0),max(16000),length=16000) 
yfit<-dnorm(xfit,mean=mean(vb),sd=sd(vb))  
yfit<-yfit*diff(hist$mids[1:2])*length(vb)
lines(xfit, yfit, col="blue",lty=5, lwd=2)

When I run these lines, I get the plot with the line and everything seems ok, but I also get this message

yfit <- yfit*diff(hist$mids[1:2])*length(vb)
Error in hist$mids : object of type 'closure' is not subsettable

I'm concerning about this error message since I don't know what it means..

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Maria
  • 13
  • 5

1 Answers1

1

You mean to do histvb$mids[1:2], not hist$mids[1:2]: histvb is the result you created in the first line: hist is a function that plots histograms. Change your line

yfit<-yfit*diff(hist$mids[1:2])*length(vb)

to

yfit<-yfit*diff(histvb$mids[1:2])*length(vb)
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • I tried that, but when I run it, it doesn't draw the line at all. Can't understand why, but I try it again, and same result: histogram OK but no line. – Maria Oct 09 '13 at 15:07
  • @Maria: First of all, why aren't you doing `plot(density(vb))`? – David Robinson Oct 09 '13 at 15:15
  • I did it at first, but I preferred the hist with the line to get a visual comparison of my data with the Gaussian distribution. – Maria Oct 09 '13 at 15:29
  • Oh sorry, I think I know what you meant, I just tried this after drawing the hist >lines(density(vb),col="red") and apparently it worked (I'll check it, I'm new at R) – Maria Oct 09 '13 at 15:32