Histogram with a dnorm-curve

168 views
Skip to first unread message

Patrick Hausmann

unread,
Mar 25, 2009, 7:50:41 AM3/25/09
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

baptiste auguie

unread,
Mar 25, 2009, 8:03:00 AM3/25/09
to Patrick Hausmann, ggp...@googlegroups.com
Hi,

perhaps you could define a custom function,

my.dnorm <- function(x, mean = 0, sd = 1, log = FALSE, scale=1)
scale*dnorm(x, mean = 0, sd = 1, log = FALSE)

to which you can pass a scaling factor,

stat_function(fun = my.dnorm, args = list(m=mean(df$u),
+ sd=sd(df$u),scale = max(histdata$count)),
colour = "blue", size=1.0)



baptiste
--

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________

hadley wickham

unread,
Mar 25, 2009, 8:43:32 AM3/25/09
to Patrick Hausmann, ggplot2

Instead of scaling the density overlay, it's probably easier to scale
the underlying histogram. Just use ggplot(df, aes(x=u, y =
..density..) )

Hadley

--
http://had.co.nz/

baptiste auguie

unread,
Mar 25, 2009, 8:48:42 AM3/25/09
to Patrick Hausmann, ggp...@googlegroups.com
a copy n' paste error slipped in the example i posted earlier, the
custom function should read,

my.dnorm <- function(x, mean = 0, sd = 1, log = FALSE, scale=1) scale
* dnorm(x, mean , sd , log )


p <- p + stat_function(fun = my.dnorm, args = list(mean=mean(df$u),
sd=sd(df$u),scale=4), colour = "blue", size=1.0)
p

sorry about that,

baptiste
Reply all
Reply to author
Forward
0 new messages