Skip to content

Allow stat_bin() to compute over single-unique-value data #3047

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

Merged
merged 6 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* `coord_sf()`, `coord_map()`, and `coord_polar()` now squash `-Inf` and `Inf`
into the min and max of the plot (@yutannihilation, #2972).

* `stat_bin()` now handles data with only one unique value (@yutannihilation #3047).

# ggplot2 3.1.0

## Breaking changes
Expand Down
3 changes: 3 additions & 0 deletions R/bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ bin_breaks_bins <- function(x_range, bins = 30, center = NULL,
bins <- as.integer(bins)
if (bins < 1) {
stop("Need at least one bin.", call. = FALSE)
} else if (zero_range(x_range)) {
# 0.1 is the same width as the expansion `expand_default()` gives for 0-width data
width <- 0.1
} else if (bins == 1) {
width <- diff(x_range)
boundary <- x_range[1]
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-stat-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ test_that("breaks are transformed by the scale", {
expect_equal(out2$xmin, sqrt(c(1, 2.5)))
})

test_that("geom_histogram() can be drawn over a 0-width range (#3043)", {
df <- data_frame(x = rep(1, 100))
out <- layer_data(ggplot(df, aes(x)) + geom_histogram())

expect_equal(nrow(out), 1)
expect_equal(out$xmin, 0.95)
expect_equal(out$xmax, 1.05)
})

# Underlying binning algorithm --------------------------------------------

comp_bin <- function(df, ...) {
Expand Down