-
Notifications
You must be signed in to change notification settings - Fork 2k
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 functions in breaks
argument to stat_bin()
#4561
Comments
A proposed implementation was submitted here: #4560 @aijordan Since you already made a PR there's no need to close it unless you feel it has a problem or discussion/review arrives at the conclusion that it's not workable. But feature requests should always be discussed in an issue first, as the request may be reasonable even if the particular implementation doesn't pass review. |
This feature request might also be a nice opportunity to allow rlang lambda notation for not only the |
@teunbrand Could you provide an example of what this would do and how it would work? |
Sure; it won't work for This already allowed (the function itself isn't very exciting): library(ggplot2)
df <- data.frame(
x = c(rexp(1000), rexp(1000, 5)),
study = c(rep("A", 1000), rep("B", 1000))
)
ggplot(df, aes(x)) +
geom_histogram(binwidth = function(x) {diff(range(x)/20)}) It is currently not allowed to write the function to the ggplot(df, aes(x)) +
geom_histogram(binwidth = ~ diff(range(.x))/20)
#> Warning: Computation failed in `stat_bin()`:
#> `width` must be a numeric scalar ggplot2 already has an internal function to convert formulas to functions when appropriate, introduced in v3.3.4. ggplot(df, aes(x)) +
geom_histogram(binwidth = ggplot2:::allow_lambda(~ diff(range(.x))/20)) Created on 2021-07-22 by the reprex package (v1.0.0) I'd propose to apply the
|
Thanks, if there is interest in this, I could reopen PR #4560 with included rlang lambda function notation support for both I feel I should point out that function input for library(ggplot2)
df <- data.frame(
x = c(rexp(1000), rexp(1000, 5)),
study = c(rep("A", 1000), rep("B", 1000))
)
ggplot(df, aes(x)) +
geom_histogram(
mapping = aes(fill = study),
binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3)),
boundary = 0)
#> Warning: position_stack requires non-overlapping x intervals Another issue with unequal bin widths arises when using ggplot(df, aes(x)) + geom_freqpoly(aes(y = ..density..), bins = 20) ggplot(df, aes(x)) +
geom_freqpoly(
mapping = aes(y = ..density..),
breaks = c(0, 0.1, 0.3, 0.6, 1, 1.5, 3, max(df$x))) It seems that padding is not optional for ggplot(df, aes(x)) +
stat_bin(
mapping = aes(y = ..density..),
geom = GeomPath,
breaks = c(-0.01, 0, 0.1, 0.3, 0.6, 1, 1.5, 3, max(df$x), max(df$x) + 0.01)) Created on 2021-07-23 by the reprex package (v2.0.0) |
@aijordan a PR with those changes would be great thanks! Do you also want to look into the freqpoly padding issue? |
Seems to me that a quick "solution" would be to have That is, change the following if-condition in
Then, custom padding via the I guess anything more than this quick fix probably requires changes to |
A small change would reduce the perceived padding in general: The function However, they seem to be used implicitly somewhere to determine the range of x values that are (invisibly) displayed. Here are two of the previous examples, when library(ggplot2)
set.seed(20220617)
df <- data.frame(
x = c(rexp(1000), rexp(1000, 5)),
study = c(rep("A", 1000), rep("B", 1000))
)
ggplot(df, aes(x)) + geom_freqpoly(aes(y = ..density..), bins = 20) ggplot(df, aes(x)) +
geom_freqpoly(
mapping = aes(y = ..density..),
breaks = c(0, 0.1, 0.3, 0.6, 1, 1.5, 3, max(df$x))) Created on 2022-06-17 by the reprex package (v2.0.1) @clauswilke Do you prefer a single or two separate pull requests, or should the padding be discussed in a new issue anyway? There is the additional question, whether |
Considering this fixed by #5963. Padding can be discussed in a new issue if so desired. |
Allowing
breaks
instat_bin()
to accept functions would be useful in the context of grouping structures.The changes would also enable facet histograms for estimators with variable bin width, e.g, equal area of all blocks, or the "essential histogram" (doi:10.1093/biomet/asz081). The same effect (with
facet_*
) is of course already possible usinggeom_rect()
.The required code additions would be minimal and follow the same idea as #1890
A simple illustration how this could work is given below:
The text was updated successfully, but these errors were encountered: