Skip to content

Commit

Permalink
Docs/example for non-equal-width bins
Browse files Browse the repository at this point in the history
  • Loading branch information
mattansb committed Oct 21, 2024
1 parent ddd207e commit 8445408
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 12 additions & 0 deletions R/geom-histogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
#' ggplot(diamonds, aes(price, after_stat(density), colour = cut)) +
#' geom_freqpoly(binwidth = 500)
#'
#'
#' # When using the non-equal-width bins, we need to set the area of the bars to
#' # represent the counts (not the height).
#' # Here we're using 10 equi-probable bins:
#' price_bins <- quantile(diamonds$price, probs = seq(0, 1, length = 11))
#'
#' ggplot(diamonds, aes(price)) +
#' geom_histogram(breaks = price_bins, color = "black") # wrong (height = count)
#'
#' ggplot(diamonds, aes(price, after_stat(count / width))) +
#' geom_histogram(breaks = price_bins, color = "black") # area = count
#'
#' if (require("ggplot2movies")) {
#' # Often we don't want the height of the bar to represent the
#' # count of observations, but the sum of some other variable.
Expand Down
2 changes: 1 addition & 1 deletion R/stat-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' density = "density of points in bin, scaled to integrate to 1.",
#' ncount = "count, scaled to a maximum of 1.",
#' ndensity = "density, scaled to a maximum of 1.",
#' width = "widths of bins."
#' 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."
#' )
#'
#' @section Dropped variables:
Expand Down
14 changes: 13 additions & 1 deletion man/geom_histogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8445408

Please sign in to comment.