Hi Adam:
It appears that y = ..count../sum(..count..) works in this case. Compare
ggplot(diamonds, aes(x = cut, y = ..count..)) +
geom_histogram() +
facet_grid(clarity ~ color)
ggplot(diamonds, aes(x = cut, y = ..count../sum(count))) +
geom_histogram() +
facet_grid(clarity ~ color)
These look the same to me except for the y-axis scaling. This would
suggest that, when faceting is involved, the current behavior is
** y = ..count../sum(count) works for bar charts (i.e., when the
x-variable is a factor)
** y = k * ..density.. works for histograms (where the bins are of
fixed width k)
The above lines of code work the same when geom_histogram() is
replaced by geom_bar(). I would suggest raising an issue about this
because the 'rules' shouldn't depend on whether or not one is
faceting.
Technically, if you have a binwidth of 1 in a histogram, where x is
numeric, then a density histogram should be identical to a relative
frequency histogram. In ggplot2,
..density.. = ..count../(N * binwidth),
where N is the sample size ( = sum(..count..) ), and by default,
binwidth = (x_max - x_min)/30. Therefore, when you set the binwidth in
geom_histogram(), multiplying both sides of the above equation by
binwidth will give you the relative frequency. Moreover, when the
binwidth is 1, a density histogram is a relative frequency histogram.
This is easy to see by example:
g <- qplot(rnorm(100), geom = "histogram", binwidth = 1.5)
g2 <- ggplot_build(g)
g2$data
Pay attention to the count and density columns, and note that the
binwidths are 1.5.
In the case where x is a factor, the binwidth trick doesn't work as
pointed out in your example, but the relative frequency definition
does. Why this works for factor but not numeric x in the presence of
faceting I don't know; perhaps you should raise this as an issue to
the developers:
https://github.com/hadley/ggplot2/issues
Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
ggplot2+u...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>