Skip to content

Commit d17427c

Browse files
committed
stat computes quantiles
1 parent 3403811 commit d17427c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

R/stat-ydensity.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@ StatYdensity <- ggproto("StatYdensity", Stat,
118118
}
119119
dens$width <- width
120120

121+
if (!is.null(draw_quantiles)) {
122+
if (!(all(draw_quantiles >= 0) && all(draw_quantiles <= 1))) {
123+
cli::cli_abort("{.arg draw_quantiles} must be between 0 and 1.")
124+
}
125+
if (!is.null(data[["weight"]]) || !all(data[["weight"]] == 1)) {
126+
cli::cli_warn(
127+
"{.arg draw_quantiles} for weighted data is not implemented."
128+
)
129+
}
130+
quants <- quantile(data$y, probs = draw_quantiles)
131+
quants <- data_frame0(
132+
y = unname(quants),
133+
quantile = draw_quantiles
134+
)
135+
136+
# Interpolate other metrics
137+
for (var in setdiff(names(dens), names(quants))) {
138+
quants[[var]] <-
139+
approx(dens$y, dens[[var]], xout = quants$y, ties = "ordered")$y
140+
}
141+
142+
dens <- vec_slice(dens, !dens$y %in% quants$y)
143+
dens <- vec_c(dens, quants)
144+
}
145+
121146
dens
122147
},
123148

0 commit comments

Comments
 (0)