Skip to content

Treatment options for zeroes in histograms #6139

New issue

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

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

Already on GitHub? # to your account

Merged
merged 6 commits into from
Oct 30, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #3449.

Briefly, it adds a keep.zeroes argument to stat_bin() that controls what happens to zero-count bins.
Rather than trim = TRUE/FALSE, I think a third option to only preserve the middle zero-count bins is useful.
Reprex from issue, demonstrating all 3 options:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
set.seed(9416)
data <- data.frame(a = rchisq(1000, df = 3),
                   b = rchisq(1000, df = 1),
                   c = rchisq(1000, df = 10)) |>
  tidyr::gather()

p <- ggplot(data, aes(x = value)) +
  facet_wrap(~ key, ncol = 1)

p + geom_histogram(colour = 'red', keep.zeroes = "all") +
  ggtitle('keep.zeroes = "all"')
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

p + geom_histogram(colour = 'red', keep.zeroes = "none") +
  ggtitle('keep.zeroes = "none"')
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Please note the absence of a gap in the 3rd panel at the bottom right.

p + geom_histogram(colour = 'red', keep.zeroes = "inner") +
  ggtitle('keep.zeroes = "inner"')
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2024-10-15 with reprex v2.1.1

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit b29b831 into tidyverse:main Oct 30, 2024
13 checks passed
@teunbrand teunbrand deleted the histogram_drop_zeroes branch October 30, 2024 11:30
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding trim for StatBin
2 participants