Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions R/stat-bin.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
#' @param breaks Alternatively, you can supply a numeric vector giving
#' the bin boundaries. Overrides `binwidth`, `bins`, `center`,
#' and `boundary`.
#'
#' Can also be a function that returns a numeric vector of bin boundaries
#' calculated from unscaled x. Here, "unscaled x"
#' refers to the original x values in the data, before application of any
#' scale transformation. When specifying a function along with a grouping
#' structure, the function will be called once per group.
#' @param closed One of `"right"` or `"left"` indicating whether right
#' or left edges of bins are included in the bin.
#' @param pad If `TRUE`, adds empty bins at either end of x. This ensures
Expand Down Expand Up @@ -143,6 +149,9 @@ StatBin <- ggproto("StatBin", Stat,
width = NULL) {
x <- flipped_names(flipped_aes)$x
if (!is.null(breaks)) {
if (is.function(breaks)) {
breaks <- breaks(data[[x]])
}
if (!scales[[x]]$is_discrete()) {
breaks <- scales[[x]]$transform(breaks)
}
Expand Down
8 changes: 7 additions & 1 deletion man/geom_histogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/stat_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test-stat-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ test_that("can use breaks argument", {
expect_equal(out$count, c(1, 2))
})

test_that("breaks computes bin boundaries for function input", {
df <- data.frame(x = c(0, 0, 0, 1:3))
out <- layer_data(ggplot(df, aes(x)) +
geom_histogram(breaks = function(x) c(0, 0.5, 2.5, 7.5)))

expect_equal(out$count, c(3, 2, 1))
})

test_that("fuzzy breaks are used when cutting", {
df <- data_frame(x = c(-1, -0.5, -0.4, 0))
p <- ggplot(df, aes(x)) +
Expand Down