Skip to content

Expand on docs/example for cases with non-equal-width bins in stat_bin() #6151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading