Skip to content

geom_histogram does not warn about absurdly large number of bins #4131

Closed

Description

@ning-y

When the range of the data is large and the binwidth is small, such that an absurdly large number of bins are required to plot the histogram, geom_histogram fails with an uninformative warning.

> ggplot(tibble(x=c(1, 1e12)), aes(x)) + geom_histogram(binwidth=1)
Warning message:
Computation failed in `stat_bin()`:
'by' argument is much too small 

This is in contrast to the case where a large (but not absurdly so) number of bins are required. In this case, a sensible and descriptive warning message is thrown.

> ggplot(tibble(x=c(1, 1e6)), aes(x)) + geom_histogram(binwidth=1)
Warning message:
Computation failed in `stat_bin()`:
The number of histogram bins must be less than 1,000,000.
Did you make `binwidth` too small? 

Which is strange, because one would expect that 1e12 breaks would behave the same as 1e6 breaks, based on the snippet below:

ggplot2/R/bin.R

Lines 90 to 96 in dcad61a

if (length(breaks) == 1) {
# In exceptionally rare cases, the above can fail and produce only a
# single break (see issue #3606). We fix this by adding a second break.
breaks <- c(breaks, breaks + width)
} else if (length(breaks) > 1e6) {
abort("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?")
}

I think that the warning message for e.g. 1e6 breaks should be consistent with that for 1e12 breaks.


My use case: I have data with range of about [0, 1e23]. Most of the data is distributed at around [0, 5] and the distribution there is shown best at a bin width of about 0.1. Following the advice written here, I intended to plot the whole histogram from [0, 1e23] with bin width 0.1, but zoom in on [0, 5] using ggforce::facet_zoom. Obviously, that didn't work. My workaround now is to subset the data from [0, 1e23] to [0, 1e5], then doing the facet_zoom.

Activity

  1. yutannihilation commented on Jul 11, 2020

    @yutannihilation
    Member

    The difference is that it fails during or after calculating breaks. The former gives up calculation right before the code chunk you pointed:

    breaks <- seq(origin, max_x, width)

    I too think it's nice if this warns a bit more nicely, but I'm not sure if we can predict seq()'s error without actually executing it...

    seq(0.5, 1e+12, 1)
    #> Error in seq.default(0.5, 1e+12, 1): 'by' argument is much too small

    Created on 2020-07-11 by the reprex package (v0.3.0)

  2. yutannihilation commented on Jul 11, 2020

    @yutannihilation
    Member

    I'm not sure if we can predict seq()'s error without actually executing it...

    Ah, maybe not so difficult?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions