Created
January 31, 2022 15:40
-
-
Save cddesja/42541539ff667fe0d8964f53055f5c17 to your computer and use it in GitHub Desktop.
Better binning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified version of StatBin2 inherits from StatBin, except for an | |
# additional 2nd last line in compute_group() function | |
StatBin2 <- ggproto( | |
"StatBin2", | |
StatBin, | |
compute_group = function (data, scales, binwidth = NULL, bins = NULL, | |
center = NULL, boundary = NULL, | |
closed = c("right", "left"), pad = FALSE, | |
breaks = NULL, origin = NULL, right = NULL, | |
drop = NULL, width = NULL) { | |
if (!is.null(breaks)) { | |
if (!scales$x$is_discrete()) { | |
breaks <- scales$x$transform(breaks) | |
} | |
bins <- ggplot2:::bin_breaks(breaks, closed) | |
} | |
else if (!is.null(binwidth)) { | |
if (is.function(binwidth)) { | |
binwidth <- binwidth(data$x) | |
} | |
bins <- ggplot2:::bin_breaks_width(scales$x$dimension(), binwidth, | |
center = center, boundary = boundary, | |
closed = closed) | |
} | |
else { | |
bins <- ggplot2:::bin_breaks_bins(scales$x$dimension(), bins, | |
center = center, boundary = boundary, | |
closed = closed) | |
} | |
res <- ggplot2:::bin_vector(data$x, bins, weight = data$weight, pad = pad) | |
# drop 0-count bins completely before returning the dataframe | |
res <- res[res$count > 0, ] | |
res | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment