Skip to content

Commit f1dda8c

Browse files
committed
geom draws quantiles, not compute them
1 parent d17427c commit f1dda8c

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

R/geom-violin.R

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
geom_violin <- function(mapping = NULL, data = NULL,
9292
stat = "ydensity", position = "dodge",
9393
...,
94-
draw_quantiles = NULL,
9594
trim = TRUE,
9695
bounds = c(-Inf, Inf),
9796
scale = "area",
@@ -110,7 +109,6 @@ geom_violin <- function(mapping = NULL, data = NULL,
110109
params = list2(
111110
trim = trim,
112111
scale = scale,
113-
draw_quantiles = draw_quantiles,
114112
na.rm = na.rm,
115113
orientation = orientation,
116114
bounds = bounds,
@@ -144,7 +142,7 @@ GeomViolin <- ggproto("GeomViolin", Geom,
144142
flip_data(data, params$flipped_aes)
145143
},
146144

147-
draw_group = function(self, data, ..., draw_quantiles = NULL, flipped_aes = FALSE) {
145+
draw_group = function(self, data, ..., flipped_aes = FALSE) {
148146
data <- flip_data(data, flipped_aes)
149147
# Find the points for the line to go all the way around
150148
data <- transform(data,
@@ -164,26 +162,15 @@ GeomViolin <- ggproto("GeomViolin", Geom,
164162
newdata <- flip_data(newdata, flipped_aes)
165163

166164
# Draw quantiles if requested, so long as there is non-zero y range
167-
if (length(draw_quantiles) > 0 & !scales::zero_range(range(data$y))) {
168-
if (!(all(draw_quantiles >= 0) && all(draw_quantiles <= 1))) {
169-
cli::cli_abort("{.arg draw_quantiles} must be between 0 and 1.")
170-
}
165+
if ("quantile" %in% names(newdata)) {
166+
167+
quantiles <- newdata[!is.na(newdata$quantile),]
168+
quantiles$group <- match(quantiles$quantile, unique(quantiles$quantile))
171169

172-
# Compute the quantile segments and combine with existing aesthetics
173-
quantiles <- create_quantile_segment_frame(data, draw_quantiles)
174-
aesthetics <- data[
175-
rep(1, nrow(quantiles)),
176-
setdiff(names(data), c("x", "y", "group")),
177-
drop = FALSE
178-
]
179-
aesthetics$alpha <- rep(1, nrow(quantiles))
180-
both <- vec_cbind(quantiles, aesthetics)
181-
both <- both[!is.na(both$group), , drop = FALSE]
182-
both <- flip_data(both, flipped_aes)
183-
quantile_grob <- if (nrow(both) == 0) {
170+
quantile_grob <- if (nrow(quantiles) == 0) {
184171
zeroGrob()
185172
} else {
186-
GeomPath$draw_panel(both, ...)
173+
GeomPath$draw_panel(quantiles, ...)
187174
}
188175

189176
ggname("geom_violin", grobTree(

0 commit comments

Comments
 (0)