-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow stat_bin() to compute over single-unique-value data #3047
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
Allow stat_bin() to compute over single-unique-value data #3047
Conversation
The default range for scales without data range is only 0.1 in the latest version of ggplot2. Maybe use the same value for default bin width? library(ggplot2)
ggplot(data.frame(), aes(x = 1, y = 0)) + geom_point() Created on 2018-12-30 by the reprex package (v0.2.1) |
Thanks, I didn't notice that. I still don't understand the details yet, but that 0.1 seems to come from this expansion, which is under control of Lines 153 to 155 in e9d4e5d
library(ggplot2)
ggplot(data.frame(), aes(x = 1, y = 0)) +
geom_point() +
coord_cartesian(expand = FALSE) Created on 2018-12-31 by the reprex package (v0.2.1) Here is the result with library(ggplot2)
library(patchwork)
p1 <- ggplot(data.frame(), aes(x = 1, y = 2)) +
geom_point() +
ggtitle("stat_identity()")
d <- data.frame(x = c(1, 1))
p2 <- ggplot(d, aes(x = x, y = stat(count))) +
geom_point(stat = "bin") +
ggtitle("stat_bin()")
p1 / p2
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. Created on 2018-12-31 by the reprex package (v0.2.1) |
I realize that the expansion will be added once more for |
Now I'm convinced that the binwidth for 0-width range should be the same value as the width of the expansion for 0-width range, 0.1. I added the commits and updated the description. |
@clauswilke Could you review this again? |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Fixes #3043.
stat_bin()
uses the width of the range ofx
to calculate the binwidth. So, it fails whenx
contains only one unique value, because the range is 0-width. Similaly toresolution()
, we need to treat zero-ranges specially.ggplot2/R/utilities-resolution.r
Lines 20 to 22 in 7f13dfa
As suggested in #3047 (comment), the width of the range should be 0.1, for consistency with the width of the expansion
expand_default()
gives for the 0-width range.Created on 2019-01-03 by the reprex package (v0.2.1)