Patrick Hausmann
unread,Mar 25, 2009, 7:50:41 AM3/25/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ggplot2
Dear list,
I am trying to overlay a histogram with a dnorm-curve.
Hadley pointed me in the right direction (stat_function), but I still
have a problem:
# This works perfect:
u <- c(1.88, 1.93, 1.98, 2.09, 2.09, 2.11,
2.13, 2.15, 2.18, 2.19, 2.14, 2.31,
2.35, 2.45, 2.62, 3.17, 3.25, 3.63)
v <- c(rep(1,9), rep(2,9))
df <- data.frame(cbind(u, v))
p <- ggplot(df, aes(x=u) )
p <- p + geom_histogram(colour = "black", fill = "yellow", binwidth =
0.25)
p <- p + scale_x_continuous(limits=c(0, 5), breaks=c(0:5) ) + theme_bw
()
p <- p + stat_function(fun = dnorm, args = list(m=mean(df$u),
sd=sd(df$u)), colour = "blue", size=1.0)
p
# stat_function is exactly what I was looking for - but
# I would like to get something like this:
histdata <- hist(df$u,freq=TRUE)
curve(max(histdata$count)*dnorm(x,mean=mean(df$u),sd=sd(df
$u)),add=TRUE)
# the crutial point is 'max(histdata$count)'...how can I solve this
with ggplot2??
Thanks for any help!!
Patrick