(1) Save the plot to an object.
(2) Use ggplot_build() to extract the counts. An example below shows how.
(3) There is a default binwidth, but it doesn't mean that there will be exactly
30 bars every time; the shape of the data has something to do with how
many bins are created. You can always establish your own binwidth as
an argument to geom_histogram().
Example:
d <- data.frame(x = rnorm(3000))
g <- ggplot(d, aes(x, y = ..count../sum(..count..))) + geom_histogram()
> str(ggplot_build(g))
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
List of 6
$ data :List of 1
..$ 1:List of 1
.. ..$ :'data.frame': 33 obs. of 11 variables:
.. .. ..$ y : num [1:33] 0 0.000667 0.001 0.001667 0.005 ...
.. .. ..$ ymin : num [1:33] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ ymax : num [1:33] 0 0.000667 0.001 0.001667 0.005 ...
.. .. ..$ x : num [1:33] -3.46 -3.24 -3.02 -2.79 -2.57 ...
.. .. ..$ xmin : num [1:33] -3.57 -3.35 -3.13 -2.9 -2.68 ...
.. .. ..$ xmax : num [1:33] -3.35 -3.13 -2.9 -2.68 -2.46 ...
.. .. ..$ count : num [1:33] 0 2 3 5 15 19 30 43 61 103 ...
.. .. ..$ ndensity: num [1:33] 0 0.00707 0.0106 0.01767 0.053 ...
.. .. ..$ ncount : num [1:33] 0 0.00707 0.0106 0.01767 0.053 ...
.. .. ..$ density : num [1:33] 0 0.00298 0.00448 0.00746 0.02239 ...
.. .. ..$ group : num [1:33] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "dim")= int [1:2] 1 1
<snip>
# The key element in this case is the count component, but it's nested
# four layers deep. To access it, use
> ggplot_build(g)$data[[1]][[1]]$count
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
[1] 0 2 3 5 15 19 30 43 61 103 130 164 210 197 243 283 236 252 232
[20] 197 188 122 100 61 33 31 18 7 6 5 2 2 0
# There are 33 bins in this histogram. Now specify the binwidth:
h <- ggplot(d, aes(x, y = ..count../sum(..count..))) +
geom_histogram(color = 'orange', binwidth = 0.5)
h
ggplot_build(h)$data[[1]][[1]]$count
[1] 0 3 17 56 130 286 438 578 549 452 300 115 55 15 6 0
Specifying binwidth = 0.5 in this example reduces the histogram to 16 bins.
Different binwidths will obviously change the number of bins and the shape of
the histogram.
BTW, if you get an unevenly rendered histogram, the issue is known to
the developers. Also, the ggplot_build() function may disappear in
some future version, so caveat emptor.
HTH,
Dennis
> --
> 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
>
Hello Brandon,sorry for the probably dumb question, but to what library belongs the bin function?I can not execute it in my R version...tnx,
G.
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility