Skip to content

Commit 6464aad

Browse files
committed
quantile drawing is controlled by graphical params
1 parent 2fec60e commit 6464aad

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

R/geom-violin.R

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#' finite, boundary effect of default density estimation will be corrected by
2222
#' reflecting tails outside `bounds` around their closest edge. Data points
2323
#' outside of bounds are removed with a warning.
24+
#' @param quantile.colour,quantile.color,quantile.linewidth,quantile.linetype
25+
#' Default aesthetics for the quantile lines. Set to `NULL` to inherit from
26+
#' the data's aesthetics. Set `quantile.linetype = 1` for regular quantiles.
2427
#' @export
2528
#' @references Hintze, J. L., Nelson, R. D. (1998) Violin Plots: A Box
2629
#' Plot-Density Trace Synergism. The American Statistician 52, 181-184.
@@ -91,11 +94,44 @@ geom_violin <- function(mapping = NULL, data = NULL,
9194
...,
9295
trim = TRUE,
9396
bounds = c(-Inf, Inf),
97+
quantile.colour = NULL,
98+
quantile.color = NULL,
99+
quantile.linetype = 0L,
100+
quantile.linewidth = NULL,
101+
draw_quantiles = deprecated(),
94102
scale = "area",
95103
na.rm = FALSE,
96104
orientation = NA,
97105
show.legend = NA,
98106
inherit.aes = TRUE) {
107+
108+
extra <- list()
109+
if (lifecycle::is_present(draw_quantiles)) {
110+
deprecate_soft0(
111+
"3.6.0",
112+
what = "geom_violin(draw_quantiles)",
113+
with = "geom_violin(quantiles.linetype)"
114+
)
115+
check_numeric(draw_quantiles)
116+
117+
# Pass on to stat when stat accepts 'quantiles'
118+
stat <- check_subclass(stat, "Stat", current_call(), caller_env())
119+
if ("quantiles" %in% stat$parameters()) {
120+
extra$quantiles <- draw_quantiles
121+
}
122+
123+
# Turn on quantile lines
124+
if (!is.null(quantile.linetype)) {
125+
quantile.linetype <- max(quantile.linetype, 1)
126+
}
127+
}
128+
129+
quantile_gp <- list(
130+
colour = quantile.color %||% quantile.colour,
131+
linetype = quantile.linetype,
132+
linewidth = quantile.linewidth
133+
)
134+
99135
layer(
100136
data = data,
101137
mapping = mapping,
@@ -110,6 +146,8 @@ geom_violin <- function(mapping = NULL, data = NULL,
110146
na.rm = na.rm,
111147
orientation = orientation,
112148
bounds = bounds,
149+
quantile_gp = quantile_gp,
150+
!!!extra,
113151
...
114152
)
115153
)
@@ -140,7 +178,7 @@ GeomViolin <- ggproto("GeomViolin", Geom,
140178
flip_data(data, params$flipped_aes)
141179
},
142180

143-
draw_group = function(self, data, ..., flipped_aes = FALSE) {
181+
draw_group = function(self, data, ..., quantile_gp = list(linetype = 0), flipped_aes = FALSE) {
144182
data <- flip_data(data, flipped_aes)
145183
# Find the points for the line to go all the way around
146184
data <- transform(data,
@@ -159,25 +197,28 @@ GeomViolin <- ggproto("GeomViolin", Geom,
159197
newdata <- vec_rbind0(newdata, newdata[1,])
160198
newdata <- flip_data(newdata, flipped_aes)
161199

162-
# Draw quantiles if requested, so long as there is non-zero y range
163-
if ("quantile" %in% names(newdata)) {
200+
violin_grob <- GeomPolygon$draw_panel(newdata, ...)
164201

165-
quantiles <- newdata[!is.na(newdata$quantile),]
166-
quantiles$group <- match(quantiles$quantile, unique(quantiles$quantile))
202+
if (!"quantile" %in% names(newdata) ||
203+
all(quantile_gp$linetype == 0) ||
204+
all(quantile_gp$linetype == "blank")) {
205+
return(ggname("geom_violin", violin_grob))
206+
}
167207

168-
quantile_grob <- if (nrow(quantiles) == 0) {
169-
zeroGrob()
170-
} else {
171-
GeomPath$draw_panel(quantiles, ...)
172-
}
208+
# Draw quantiles if requested, so long as there is non-zero y range
209+
quantiles <- newdata[!is.na(newdata$quantile),]
210+
quantiles$group <- match(quantiles$quantile, unique(quantiles$quantile))
211+
quantiles$linetype <- quantile_gp$linetype %||% quantiles$linetype
212+
quantiles$linewidth <- quantile_gp$linewidth %||% quantiles$linewidth
213+
quantiles$colour <- quantile_gp$colour %||% quantiles$colour
173214

174-
ggname("geom_violin", grobTree(
175-
GeomPolygon$draw_panel(newdata, ...),
176-
quantile_grob)
177-
)
215+
quantile_grob <- if (nrow(quantiles) == 0) {
216+
zeroGrob()
178217
} else {
179-
ggname("geom_violin", GeomPolygon$draw_panel(newdata, ...))
218+
GeomPath$draw_panel(quantiles, ...)
180219
}
220+
221+
ggname("geom_violin", grobTree(violin_grob, quantile_grob))
181222
},
182223

183224
draw_key = draw_key_polygon,

0 commit comments

Comments
 (0)