Skip to content

Commit 8445408

Browse files
committed
Docs/example for non-equal-width bins
1 parent ddd207e commit 8445408

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

R/geom-histogram.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
#' ggplot(diamonds, aes(price, after_stat(density), colour = cut)) +
6464
#' geom_freqpoly(binwidth = 500)
6565
#'
66+
#'
67+
#' # When using the non-equal-width bins, we need to set the area of the bars to
68+
#' # represent the counts (not the height).
69+
#' # Here we're using 10 equi-probable bins:
70+
#' price_bins <- quantile(diamonds$price, probs = seq(0, 1, length = 11))
71+
#'
72+
#' ggplot(diamonds, aes(price)) +
73+
#' geom_histogram(breaks = price_bins, color = "black") # wrong (height = count)
74+
#'
75+
#' ggplot(diamonds, aes(price, after_stat(count / width))) +
76+
#' geom_histogram(breaks = price_bins, color = "black") # area = count
77+
#'
6678
#' if (require("ggplot2movies")) {
6779
#' # Often we don't want the height of the bar to represent the
6880
#' # count of observations, but the sum of some other variable.

R/stat-bin.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' density = "density of points in bin, scaled to integrate to 1.",
3232
#' ncount = "count, scaled to a maximum of 1.",
3333
#' ndensity = "density, scaled to a maximum of 1.",
34-
#' width = "widths of bins."
34+
#' width = "widths of bins. Use with `after_stat(count / width)` to obtain bars with _areas_ representing counts (e.g., with non-equal-width bins). See example."
3535
#' )
3636
#'
3737
#' @section Dropped variables:

man/geom_histogram.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)