I'd like to superimpose a histogram and a density plot, both using
..count.. as the y variable,
library(ggplot2)
d <- data.frame(x=rnorm(1e4, 10, 1))
foo <- function(n)
ggplot(d, aes(x=x)) +
geom_histogram(aes(y = ..count..), binwidth=diff(range(d$x))/n,
fill="grey50", colour="grey40") +
geom_line(aes(y = ..count..), stat="density",
size = 1, colour="red", linetype=2) +
scale_x_continuous("x") +
scale_y_continuous("Count") +
theme_bw()
foo(10) # density and histogram roughly of same height
foo(40) # histogram much below
My very naive understanding of histograms and density plots using
..count.. would have been that no matter the binwidth, the area under
the curve and the histogram should be similar. This isn't the case
here; should I adjust some parameters, or is this a bad idea
altogether?
Best regards,
baptiste
That's true when you plot density, not count.
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
Dear list,
I'd like to superimpose a histogram and a density plot, both using
..count.. as the y variable,
library(ggplot2)
d <- data.frame(x=rnorm(1e4, 10, 1))
foo <- function(n)
ggplot(d, aes(x=x)) +
geom_histogram(aes(y = ..count..), binwidth=diff(range(d$x))/n,
fill="grey50", colour="grey40") +
geom_line(aes(y = ..count..), stat="density",
size = 1, colour="red", linetype=2) +
scale_x_continuous("x") +
scale_y_continuous("Count") +
theme_bw()
foo(10) # density and histogram roughly of same height
foo(40) # histogram much below
My very naive understanding of histograms and density plots using
..count.. would have been that no matter the binwidth, the area under
the curve and the histogram should be similar. This isn't the case
here; should I adjust some parameters, or is this a bad idea
altogether?
Best regards,
baptiste
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
It also works with binwidth=1, but eventually I just removed the
density lines, they didn't bring more information.
Thanks,
baptiste