Skip to content

Commit c76b9ae

Browse files
authored
Boxplot staples (#5372)
* Implement staples * Document * Add test * Add check for staplewidth * Fix typo
1 parent 8af03a9 commit c76b9ae

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

NEWS.md

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

3+
* `geom_boxplot()` gains a new argument, `staplewidth` that can draw staples
4+
at the ends of whiskers (@teunbrand, #5126)
5+
36
* The `size` argument in `annotation_logticks()` has been deprecated in favour
47
of the `linewidth` argument (#5292).
58

R/geom-boxplot.R

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#' are significantly different.
5353
#' @param notchwidth For a notched box plot, width of the notch relative to
5454
#' the body (defaults to `notchwidth = 0.5`).
55+
#' @param staplewidth The relative width of staples to the width of the box.
56+
#' Staples mark the ends of the whiskers with a line.
5557
#' @param varwidth If `FALSE` (default) make a standard box plot. If
5658
#' `TRUE`, boxes are drawn with widths proportional to the
5759
#' square-roots of the number of observations in the groups (possibly
@@ -119,6 +121,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
119121
outlier.alpha = NULL,
120122
notch = FALSE,
121123
notchwidth = 0.5,
124+
staplewidth = 0,
122125
varwidth = FALSE,
123126
na.rm = FALSE,
124127
orientation = NA,
@@ -134,6 +137,8 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
134137
position$preserve <- "single"
135138
}
136139
}
140+
141+
check_number_decimal(staplewidth)
137142
check_bool(outliers)
138143

139144
layer(
@@ -154,6 +159,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
154159
outlier.alpha = outlier.alpha,
155160
notch = notch,
156161
notchwidth = notchwidth,
162+
staplewidth = staplewidth,
157163
varwidth = varwidth,
158164
na.rm = na.rm,
159165
orientation = orientation,
@@ -218,7 +224,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
218224
outlier.fill = NULL, outlier.shape = 19,
219225
outlier.size = 1.5, outlier.stroke = 0.5,
220226
outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5,
221-
varwidth = FALSE, flipped_aes = FALSE) {
227+
staplewidth = 0, varwidth = FALSE, flipped_aes = FALSE) {
222228
data <- check_linewidth(data, snake_class(self))
223229
data <- flip_data(data, flipped_aes)
224230
# this may occur when using geom_boxplot(stat = "identity")
@@ -282,8 +288,28 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
282288
outliers_grob <- NULL
283289
}
284290

291+
if (staplewidth != 0) {
292+
staples <- data_frame0(
293+
x = rep((data$xmin - data$x) * staplewidth + data$x, 2),
294+
xend = rep((data$xmax - data$x) * staplewidth + data$x, 2),
295+
y = c(data$ymax, data$ymin),
296+
yend = c(data$ymax, data$ymin),
297+
alpha = c(NA_real_, NA_real_),
298+
!!!common,
299+
.size = 2
300+
)
301+
staples <- flip_data(staples, flipped_aes)
302+
staple_grob <- GeomSegment$draw_panel(
303+
staples, panel_params, coord,
304+
lineend = lineend
305+
)
306+
} else {
307+
staple_grob <- NULL
308+
}
309+
285310
ggname("geom_boxplot", grobTree(
286311
outliers_grob,
312+
staple_grob,
287313
GeomSegment$draw_panel(whiskers, panel_params, coord, lineend = lineend),
288314
GeomCrossbar$draw_panel(
289315
box,

man/geom_boxplot.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 96 additions & 0 deletions
Loading

tests/testthat/test-geom-boxplot.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ test_that("boxplot draws correctly", {
9393
expect_doppelganger("outlier colours",
9494
ggplot(mtcars, aes(x = factor(cyl), y = drat, colour = factor(cyl))) + geom_boxplot(outlier.size = 5)
9595
)
96+
expect_doppelganger("staples",
97+
ggplot(mtcars, aes(x = factor(cyl), y = drat, colour = factor(cyl))) + geom_boxplot(staplewidth = 0.5)
98+
)
9699
})

0 commit comments

Comments
 (0)