Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a normal distribution line in histogram

Tags:

r

lines

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!

like image 645
zhang525986 Avatar asked Jun 18 '13 06:06

zhang525986


2 Answers

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.

like image 127
andreister Avatar answered Nov 16 '22 17:11

andreister


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

like image 25
Hong Ooi Avatar answered Nov 16 '22 19:11

Hong Ooi