Skip to content

Commit 5a61e2e

Browse files
authored
Adds bounds to geom_violin() (#5494)
Closes #5493
1 parent f74dbbe commit 5a61e2e

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

NEWS.md

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

3+
* `geom_violin()` gains a `bounds` argument analogous to `geom_density()`s (@eliocamp, #5493).
4+
35
* Legend titles no longer take up space if they've been removed by setting
46
`legend.title = element_blank()` (@teunbrand, #3587).
57

R/geom-violin.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#' to the range of the data. If `FALSE`, don't trim the tails.
1717
#' @param geom,stat Use to override the default connection between
1818
#' `geom_violin()` and `stat_ydensity()`.
19+
#' @param bounds Known lower and upper bounds for estimated data. Default
20+
#' `c(-Inf, Inf)` means that there are no (finite) bounds. If any bound is
21+
#' finite, boundary effect of default density estimation will be corrected by
22+
#' reflecting tails outside `bounds` around their closest edge. Data points
23+
#' outside of bounds are removed with a warning.
1924
#' @export
2025
#' @references Hintze, J. L., Nelson, R. D. (1998) Violin Plots: A Box
2126
#' Plot-Density Trace Synergism. The American Statistician 52, 181-184.
@@ -86,6 +91,7 @@ geom_violin <- function(mapping = NULL, data = NULL,
8691
...,
8792
draw_quantiles = NULL,
8893
trim = TRUE,
94+
bounds = c(-Inf, Inf),
8995
scale = "area",
9096
na.rm = FALSE,
9197
orientation = NA,
@@ -105,6 +111,7 @@ geom_violin <- function(mapping = NULL, data = NULL,
105111
draw_quantiles = draw_quantiles,
106112
na.rm = na.rm,
107113
orientation = orientation,
114+
bounds = bounds,
108115
...
109116
)
110117
)

R/stat-ydensity.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ stat_ydensity <- function(mapping = NULL, data = NULL,
3535
na.rm = FALSE,
3636
orientation = NA,
3737
show.legend = NA,
38-
inherit.aes = TRUE) {
38+
inherit.aes = TRUE,
39+
bounds = c(-Inf, Inf)) {
3940
scale <- arg_match0(scale, c("area", "count", "width"))
4041

4142
layer(
@@ -54,6 +55,7 @@ stat_ydensity <- function(mapping = NULL, data = NULL,
5455
scale = scale,
5556
drop = drop,
5657
na.rm = na.rm,
58+
bounds = bounds,
5759
...
5860
)
5961
)
@@ -78,7 +80,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
7880

7981
compute_group = function(self, data, scales, width = NULL, bw = "nrd0", adjust = 1,
8082
kernel = "gaussian", trim = TRUE, na.rm = FALSE,
81-
drop = TRUE, flipped_aes = FALSE) {
83+
drop = TRUE, flipped_aes = FALSE, bounds = c(-Inf, Inf)) {
8284
if (nrow(data) < 2) {
8385
if (isTRUE(drop)) {
8486
cli::cli_warn(c(
@@ -98,7 +100,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
98100
dens <- compute_density(
99101
data$y, data[["weight"]],
100102
from = range[1] - modifier * bw, to = range[2] + modifier * bw,
101-
bw = bw, adjust = adjust, kernel = kernel
103+
bw = bw, adjust = adjust, kernel = kernel, bounds = bounds
102104
)
103105

104106
dens$y <- dens$x
@@ -118,11 +120,12 @@ StatYdensity <- ggproto("StatYdensity", Stat,
118120

119121
compute_panel = function(self, data, scales, width = NULL, bw = "nrd0", adjust = 1,
120122
kernel = "gaussian", trim = TRUE, na.rm = FALSE,
121-
scale = "area", flipped_aes = FALSE, drop = TRUE) {
123+
scale = "area", flipped_aes = FALSE, drop = TRUE,
124+
bounds = c(-Inf, Inf)) {
122125
data <- flip_data(data, flipped_aes)
123126
data <- ggproto_parent(Stat, self)$compute_panel(
124127
data, scales, width = width, bw = bw, adjust = adjust, kernel = kernel,
125-
trim = trim, na.rm = na.rm, drop = drop
128+
trim = trim, na.rm = na.rm, drop = drop, bounds = bounds,
126129
)
127130
if (!drop && any(data$n < 2)) {
128131
cli::cli_warn(

man/geom_violin.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)