Skip to content

Commit 0d2023d

Browse files
authored
Violin resolution (#5402)
* Preserve mapped_discrete class * Add test * Add news bullet
1 parent f6269b0 commit 0d2023d

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `stat_ydensity()` with incomplete groups calculates the default `width`
4+
parameter more stably (@teunbrand, #5396)
5+
36
* `geom_boxplot()` gains a new argument, `staplewidth` that can draw staples
47
at the ends of whiskers (@teunbrand, #5126)
58

R/stat-ydensity.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,14 @@ StatYdensity <- ggproto("StatYdensity", Stat,
102102
)
103103

104104
dens$y <- dens$x
105-
dens$x <- mean(range(data$x))
106105

107106
# Compute width if x has multiple values
108107
if (vec_unique_count(data$x) > 1) {
108+
dens$x <- mean(range(data$x))
109109
width <- diff(range(data$x)) * 0.9
110+
} else {
111+
# Explicitly repeat to preserve data$x's mapped_discrete class
112+
dens$x <- vec_rep(data$x[1], nrow(dens))
110113
}
111114
dens$width <- width
112115

tests/testthat/test-stat-ydensity.R

+16
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,19 @@ test_that("`drop = FALSE` preserves groups with 1 observations", {
2525
)
2626
expect_equal(length(unique(ld$x)), 4)
2727
})
28+
29+
test_that("mapped_discrete class is preserved", {
30+
31+
df <- data_frame0(
32+
x = factor(rep(c("A", "C"), each = 3), c("A", "B", "C")),
33+
y = 1:6
34+
)
35+
36+
ld <- layer_data(
37+
ggplot(df, aes(x, y)) + geom_violin() +
38+
scale_x_discrete(drop = FALSE)
39+
)
40+
41+
expect_s3_class(ld$x, "mapped_discrete")
42+
expect_equal(unique(ld$x), c(1, 3))
43+
})

0 commit comments

Comments
 (0)