Repository navigation
Description
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:
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
yutannihilation commentedon Jul 11, 2020
The difference is that it fails during or after calculating breaks. The former gives up calculation right before the code chunk you pointed:
ggplot2/R/bin.R
Line 88 in dcad61a
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...Created on 2020-07-11 by the reprex package (v0.3.0)
yutannihilation commentedon Jul 11, 2020
Ah, maybe not so difficult?