From 647e5d545792d7bc35a12cec8707d80363d49077 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 27 Dec 2018 10:38:07 +0900 Subject: [PATCH 1/5] Add a failing test --- tests/testthat/test-stat-bin.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test-stat-bin.R b/tests/testthat/test-stat-bin.R index 8dc0b6f9c6..074e163104 100644 --- a/tests/testthat/test-stat-bin.R +++ b/tests/testthat/test-stat-bin.R @@ -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.5) + expect_equal(out$xmax, 1.5) +}) + # Underlying binning algorithm -------------------------------------------- comp_bin <- function(df, ...) { From 2b086318cb26e814d64cd61a8bb11cd5373dc69b Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 27 Dec 2018 10:40:17 +0900 Subject: [PATCH 2/5] Use the fixed binwidth when the range is 0-width --- R/bin.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/bin.R b/R/bin.R index 654a01f98e..60fe9a21de 100644 --- a/R/bin.R +++ b/R/bin.R @@ -97,6 +97,8 @@ 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)) { + width <- 1 } else if (bins == 1) { width <- diff(x_range) boundary <- x_range[1] From 38afc582f8ddd971ad9dc9939da552382a2eb089 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 27 Dec 2018 11:10:12 +0900 Subject: [PATCH 3/5] Add a NEWS bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 313dc17df7..101def5842 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 From a211badd1bf353190d035f05c94f9464b5667eff Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 3 Jan 2019 15:42:09 +0900 Subject: [PATCH 4/5] Use 0.1 for width --- R/bin.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/bin.R b/R/bin.R index 60fe9a21de..dd1303a3ab 100644 --- a/R/bin.R +++ b/R/bin.R @@ -98,7 +98,8 @@ bin_breaks_bins <- function(x_range, bins = 30, center = NULL, if (bins < 1) { stop("Need at least one bin.", call. = FALSE) } else if (zero_range(x_range)) { - width <- 1 + # 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] From 156fd1b0ee0ff192ee00a9da2e8467e70e543026 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 3 Jan 2019 15:44:24 +0900 Subject: [PATCH 5/5] Modify tests --- tests/testthat/test-stat-bin.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-stat-bin.R b/tests/testthat/test-stat-bin.R index 074e163104..6da9671f83 100644 --- a/tests/testthat/test-stat-bin.R +++ b/tests/testthat/test-stat-bin.R @@ -74,8 +74,8 @@ test_that("geom_histogram() can be drawn over a 0-width range (#3043)", { out <- layer_data(ggplot(df, aes(x)) + geom_histogram()) expect_equal(nrow(out), 1) - expect_equal(out$xmin, 0.5) - expect_equal(out$xmax, 1.5) + expect_equal(out$xmin, 0.95) + expect_equal(out$xmax, 1.05) }) # Underlying binning algorithm --------------------------------------------