Saturday, 15 September 2012

r - Plotting a normal curve on top of a weighted histogram -



r - Plotting a normal curve on top of a weighted histogram -

i have dataset requires me plot weighted histogram because of info

gridcellname value avg.temp y55x21 4,84 4,09 y56x17 0 5,34 y44x44 2,75 1,8 y46x38 3,68 3,4 y58x19 4,32 5,08 y59x17 0 4,8 y60x20 5,29 5,27 y61x18 0 5,15 y62x21 5,59 5,09 y63x19 3,05 5,47 y64x22 2,75 5,04 y65x25 4,82 4,54 y66x22 0 4,54

i utilize weighted histogram , breaks

h <- weighted.hist(df$avg.temp, df$value, ylim=c(0,120), breaks=c(seq(-1.5,8.5, by=0.5)))

or

ggplot(df, aes(y=value, x=temp_category)) + stat_summary(fun.y="sum", geom="bar")

this gives me nice graph unable draw normal curve on top of it. can see 2 plots, may not have info in (-1.5,-1) category still nevertheless plot , maintain yaxis range (0,120) although max values around ~40.

i have tried these 2 codes adding curve not exclusively correct

xfit<-seq(min(df$value),max(df$value)) yfit<-dnorm(xfit,mean=mean(df$value),sd=sd(df$value)) yfit <- yfit*diff(h$mids[1:2])*length(df$value) lines(xfit, yfit, col="blue", lwd=2)

this curve seems on left of plot.

ggplot(df, aes(y=value, x=temp_category)) + stat_summary(fun.y="sum", geom="bar") + stat_function(fun=dnorm, args=list(mean=mean(df$value), sd=sd(df$value)))

this gives me flat horizontal line x axis.

thank you!

r normal-distribution weighted

No comments:

Post a Comment