-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Incorrect warning about clipping after adding wide xlim values to geom_histogram #5118
Comments
Because the scale range is larger than the data range, this results in some empty, 0-count, bins at the flanks of the histograms. If these flanking bins are out-of-bounds, they get censored and dropped, which is the warning you get. We can show in the layer data that there is an empty bin at the start and end that have library(ggplot2)
set.seed(12345)
df <- data.frame(x = rnorm(5000) / 10)
p <- ggplot(data = df, aes(x = x)) +
geom_histogram() +
xlim(-1, 1)
ld <- layer_data(p)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
head(ld, 2)[, 1:8]
#> y count x xmin xmax density ncount ndensity
#> 1 0 0 NA NA -1.0000000 0 0 0
#> 2 0 0 -0.9655172 -1 -0.9310345 0 0 0
tail(ld, 2)[, 1:8]
#> y count x xmin xmax density ncount ndensity
#> 29 0 0 0.8965517 0.8620690 0.9310345 0 0 0
#> 30 0 0 0.9655172 0.9310345 NA 0 0 0 Created on 2022-12-24 by the reprex package (v2.0.1) This is all how it is supposed to work, but what I don't understand (yet) is why the breaks for the bins get calculated outside the bounds of the scale range. If you want to remedy this issue, you could use |
Wow. Brilliant answer: many thanks. I suspect I should have been able to work this out myself, perhaps if I had tried |
Brief description of the problem
If I add xlim() or limits in scale_x_continuous() using geom_histogram and setting the limits outside the range of the data I see a warning message:
Removed 2 rows containing missing values (
geom_bar()
).but in fact nothing has been removed.
True for me on:
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
and
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS
and
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
All three show ggplot2 at version 3.4.0
Reprex follows.
I hope I'm not being stupid!
The text was updated successfully, but these errors were encountered: