From 113144fa9b6c220baeb888bc631cc1e65b181d23 Mon Sep 17 00:00:00 2001 From: bersbersbers <12128514+bersbersbers@users.noreply.github.com> Date: Sat, 19 Mar 2022 09:09:31 +0100 Subject: [PATCH 1/4] Test that quantiles are drawn at expected positions (#4654) --- tests/testthat/test-geom-violin.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/testthat/test-geom-violin.R b/tests/testthat/test-geom-violin.R index d9d81b135b..38c0581284 100644 --- a/tests/testthat/test-geom-violin.R +++ b/tests/testthat/test-geom-violin.R @@ -46,6 +46,17 @@ test_that("quantiles do not fail on zero-range data", { expect_equal(length(layer_grob(p)), 1) }) +test_that("quantiles are at expected positions at zero width", { + # Symmetric density with n components and zero middle: + # 50% quantile can be drawn anywhere as long as there is density 0 + n <- 256 + density <- c(rep(2, n / 4), rep(0, n / 2), rep(2, n / 4)) / n + density.data <- data_frame(y = (1:n) / n, density = density) + line <- create_quantile_segment_frame(density.data, 0.5) + y_idx <- which.min(abs(density.data$y - line$y[1])) + expect_equal(density[y_idx], 0) +}) + # Visual tests ------------------------------------------------------------ From 47f978f6379766eb1c050e80e42498686271783d Mon Sep 17 00:00:00 2001 From: bersbersbers <12128514+bersbersbers@users.noreply.github.com> Date: Fri, 29 Oct 2021 13:22:13 +0200 Subject: [PATCH 2/4] Fix warning in geom_violin with draw_quantiles Fix "collapsing to unique 'x' values" warnings (#4654). Closes #4455 --- R/geom-violin.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/geom-violin.r b/R/geom-violin.r index fe8e710389..16371916aa 100644 --- a/R/geom-violin.r +++ b/R/geom-violin.r @@ -197,7 +197,7 @@ GeomViolin <- ggproto("GeomViolin", Geom, # Returns a data.frame with info needed to draw quantile segments. create_quantile_segment_frame <- function(data, draw_quantiles) { dens <- cumsum(data$density) / sum(data$density) - ecdf <- stats::approxfun(dens, data$y) + ecdf <- stats::approxfun(dens, data$y, ties = "ordered") ys <- ecdf(draw_quantiles) # these are all the y-values for quantiles # Get the violin bounds for the requested quantiles. From a2dc66ea4cdf7e7edd1913c136c2af17c99314cf Mon Sep 17 00:00:00 2001 From: bersbersbers <12128514+bersbersbers@users.noreply.github.com> Date: Sat, 19 Mar 2022 09:15:44 +0100 Subject: [PATCH 3/4] Test that quantiles do not issue warning (#4654) --- tests/testthat/test-geom-violin.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test-geom-violin.R b/tests/testthat/test-geom-violin.R index 38c0581284..9b6b3b1429 100644 --- a/tests/testthat/test-geom-violin.R +++ b/tests/testthat/test-geom-violin.R @@ -57,6 +57,15 @@ test_that("quantiles are at expected positions at zero width", { expect_equal(density[y_idx], 0) }) +test_that("quantiles do not issue warning", { + data <- data_frame(x = 1, y = c(0, 0.25, 0.5, 0.75, 5)) + + p <- ggplot(data, aes(x = x, y = y)) + + geom_violin(draw_quantiles = 0.5) + + expect_warning(plot(p), regexp = NA) +}) + # Visual tests ------------------------------------------------------------ From 384607bc61248756257970f6e26bb1e066685e1e Mon Sep 17 00:00:00 2001 From: bersbersbers <12128514+bersbersbers@users.noreply.github.com> Date: Sat, 19 Mar 2022 09:22:12 +0100 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 5733b1e4ef..b595f084fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* `geom_violin()` no longer issues "collapsing to unique 'x' values" warning + (@bersbersbers, #4455) + * `annotate()` now documents unsupported geoms (`geom_abline()`, `geom_hline()` and `geom_vline()`), and warns when they are requested (@mikmart, #4719)