# [R] Plot gamma distribution with relative frequency histogram

David Winsemius dwinsemius at comcast.net
Fri Dec 5 09:32:24 CET 2014

```On Dec 4, 2014, at 6:55 PM, dila radi wrote:

> Dear R users,
>
> I am looking to fit a gamma curve onto a histogram of the data.
>
> Consider dt1 as my data:
> c(203.9, 91.5, 24.5, 34.5, 164, 144, 160.5, 195, 191.5, 189,
> 133, 110.5, 155, 80.5, 250.5, 116, 145, 118.5, 406, 183.5, 142.5,
> 197, 367, 134, 153.5)
>
> I am using this code in R
> x<-dt1
> h<-hist(x,breaks=seq(0,500,25),col=rgb(0,0,1), plot=F)
> h\$counts <- h\$counts / sum(h\$counts)
> plot(h, freq=TRUE, ylab="Relative Frequency", xlab="Rainfall (mm)",
>     main = 'Histogram KBU-ML Gamma Disribution')
> est <- fitdistr(dt1,"gamma")\$estimate
> curve(dgamma(x, rate=0.02220921, shape=3.63421746),from=0, to=500,
>      main="Gamma distribution", add = T, col = "red")
>
> but the gamma fit is not fitted onto histogram as much as it supposed to
> be. How should I overcome this?
>
First read ?hist. Then set freq=FALSE. Then you will be comparing "like to like".

Otherwise you cna just hack it by multiplying the dgamma expression by 25.

> sum(h\$counts)  #before the standardization
 25

>
> Dila
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help