Skip to content

Instantly share code, notes, and snippets.

@adamhsparks
Forked from eliocamp/StatQuantileBin.R
Created April 16, 2023 11:39
Show Gist options
  • Save adamhsparks/2a3c8b85364f5383c4a0164b0a0d7e7f to your computer and use it in GitHub Desktop.
Save adamhsparks/2a3c8b85364f5383c4a0164b0a0d7e7f to your computer and use it in GitHub Desktop.
Percentogram (histogram with bins of equal number of observations)
StatQuantileBin <- ggplot2::ggproto("StatQuantileBin", ggplot2::StatBin,
default_aes = ggplot2::aes(x = ggplot2::after_stat(density), y = ggplot2::after_stat(density), weight = 1),
compute_group = function(data, scales,
binwidth = NULL, bins = 30, breaks = NULL, trim = 0,
closed = c("right", "left"), pad = FALSE,
flipped_aes = FALSE,
# The following arguments are not used, but must
# be listed so parameters are computed correctly
origin = NULL, right = NULL, drop = NULL,
width = NULL) {
x <- ggplot2::flipped_names(flipped_aes)$x
if (is.null(breaks)) { # If breaks is not provided, we need to compute them
if (!is.null(binwidth)) { # Either with binwidth
trim <- trim/2
quantiles <- seq(trim, 1 - trim, binwidth)
} else { # or the number of bins
quantiles <- seq(trim, 1 - trim, length.out = bins)
}
} else {
quantiles <- breaks
}
breaks <- quantile(data[[x]], quantiles)
keep <- !duplicated(breaks)
# If quantiles is too close, sometimes you get duplicates
breaks <- breaks[keep]
quantiles <- quantiles[keep]
bins <- ggplot2:::bin_breaks(breaks, closed)
bins <- ggplot2:::bin_vector(data[[x]], bins, weight = data$weight, pad = pad)
bins$quantile <- quantiles[-length(quantiles)]
bins$flipped_aes <- flipped_aes
ggplot2::flip_data(bins, flipped_aes)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment