Skip to content

Commit afc03e0

Browse files
authored
Fix warning in geom_violin with draw_quantiles (#4654)
Fixes #4455
1 parent 5ce5cf7 commit afc03e0

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `geom_violin()` no longer issues "collapsing to unique 'x' values" warning
4+
(@bersbersbers, #4455)
5+
36
* `annotate()` now documents unsupported geoms (`geom_abline()`, `geom_hline()`
47
and `geom_vline()`), and warns when they are requested (@mikmart, #4719)
58

R/geom-violin.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ GeomViolin <- ggproto("GeomViolin", Geom,
197197
# Returns a data.frame with info needed to draw quantile segments.
198198
create_quantile_segment_frame <- function(data, draw_quantiles) {
199199
dens <- cumsum(data$density) / sum(data$density)
200-
ecdf <- stats::approxfun(dens, data$y)
200+
ecdf <- stats::approxfun(dens, data$y, ties = "ordered")
201201
ys <- ecdf(draw_quantiles) # these are all the y-values for quantiles
202202

203203
# Get the violin bounds for the requested quantiles.

tests/testthat/test-geom-violin.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ test_that("quantiles do not fail on zero-range data", {
4646
expect_equal(length(layer_grob(p)), 1)
4747
})
4848

49+
test_that("quantiles are at expected positions at zero width", {
50+
# Symmetric density with n components and zero middle:
51+
# 50% quantile can be drawn anywhere as long as there is density 0
52+
n <- 256
53+
density <- c(rep(2, n / 4), rep(0, n / 2), rep(2, n / 4)) / n
54+
density.data <- data_frame(y = (1:n) / n, density = density)
55+
line <- create_quantile_segment_frame(density.data, 0.5)
56+
y_idx <- which.min(abs(density.data$y - line$y[1]))
57+
expect_equal(density[y_idx], 0)
58+
})
59+
60+
test_that("quantiles do not issue warning", {
61+
data <- data_frame(x = 1, y = c(0, 0.25, 0.5, 0.75, 5))
62+
63+
p <- ggplot(data, aes(x = x, y = y)) +
64+
geom_violin(draw_quantiles = 0.5)
65+
66+
expect_warning(plot(p), regexp = NA)
67+
})
68+
4969

5070
# Visual tests ------------------------------------------------------------
5171

0 commit comments

Comments
 (0)