Skip to content

Instantly share code, notes, and snippets.

@rjbgoudie
Created May 13, 2019 09:22
Show Gist options
  • Save rjbgoudie/e99cd166b1eb705dced05f9995a81916 to your computer and use it in GitHub Desktop.
Save rjbgoudie/e99cd166b1eb705dced05f9995a81916 to your computer and use it in GitHub Desktop.
stat_bin_step <- function(mapping = NULL, data = NULL,
geom = "step", position = "stack",
...,
binwidth = NULL,
bins = NULL,
center = NULL,
boundary = NULL,
breaks = NULL,
closed = c("right", "left"),
pad = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = StatBinStep,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
binwidth = binwidth,
bins = bins,
center = center,
boundary = boundary,
breaks = breaks,
closed = closed,
pad = pad,
na.rm = na.rm,
...
)
)
}
StatBinStep <- ggproto("StatBinStep", StatBin,
compute_group = function(data, scales, binwidth = NULL, bins = NULL,
center = NULL, boundary = NULL,
closed = c("right", "left"),
breaks = NULL,
# The following arguments are not used, but must
# be listed so parameters are computed correctly
origin = NULL, right = NULL, drop = NULL,
width = NULL) {
pad <- TRUE
if (!is.null(breaks)) {
if (!scales$x$is_discrete()){
breaks <- scales$x$transform(breaks)
}
bins <- bin_breaks(breaks, closed)
} else if (!is.null(binwidth)) {
if (is.function(binwidth)) {
binwidth <- binwidth(data$x)
}
bins <- 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)
}
out <- ggplot2:::bin_vector(data$x, bins, weight = data$weight, pad = pad)
out <- transform(out,
x = xmin,
y = density)
out
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment