7

I've met a weird problem that I can't figure it out totally. I'm supposed to add a normal distribution line upon a histogram.I input every step's code but after typing lines function there's no response. I don't know what's wrong.Hope anyone help me! MY code are:

grades<-mydata$Exam1
hist(grades,breaks=20,freq=T) #A correct histogram comes out.
mean(grades,na.rm=T) #there is NA in the column so I remove it when calculating mean.
[1] 75.15278

sd(grades,na.rm=T)  
[1] 16.97443 

x<-seq(0,100,0.01) 
y<-dnorm(x,mean=mean(grades,na.rm=T),sd=sd(grades,na.rm=T))
lines(x,y)#and there's no response!no line showed up!

Is anything wrong with my code? Thanks for your help!

andreister
  • 13,693
  • 2
  • 44
  • 45
zhang525986
  • 203
  • 2
  • 4
  • 6

2 Answers2

21

I assume it's R code - then try this:

grades <- mydata$Exam1
hist(grades, prob=TRUE)
curve(dnorm(x, mean=mean(grades), sd=sd(grades)), add=TRUE)

Note that if you compare normal distribution to the histogram, you probably want histogram to display probabilities rather than frequencies.

andreister
  • 13,693
  • 2
  • 44
  • 45
4

You want hist(*, freq=FALSE), not freq=TRUE.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187