Skip to content

Commit 515498e

Browse files
committed
get notches working
1 parent 8b096b9 commit 515498e

File tree

1 file changed

+41
-54
lines changed

1 file changed

+41
-54
lines changed

R/layers2traces.R

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ to_basic.GeomBoxplot <- function(data, prestats_data, layout, params, p, ...) {
206206
ynotchupper = ifelse(params$notch, notchupper, NA),
207207
notchwidth = params$notchwidth
208208
)
209-
210209
outliers <- if (length(data$outliers) && !is.na(params$outlier.shape)) {
211210
tidyr::unnest(data) %>%
212211
dplyr::mutate(
@@ -221,57 +220,6 @@ to_basic.GeomBoxplot <- function(data, prestats_data, layout, params, p, ...) {
221220
alpha = params$outlier.alpha %||% alpha
222221
)
223222
}
224-
225-
# If boxplot has notches, it needs to drawn as a polygon (instead of a crossbar/rect)
226-
# This code is adapted from GeomCrossbar$draw_panel()
227-
if (params$notch) {
228-
# TODO: where does fatten come from?
229-
fatten <- 2.5
230-
middle <- transform(
231-
box, x = xmin, xend = xmax, yend = y,
232-
size = size * fatten, alpha = NA
233-
)
234-
if (box$ynotchlower < box$ymin || box$ynotchupper > box$ymax)
235-
message("notch went outside hinges. Try setting notch=FALSE.")
236-
notchindent <- (1 - box$notchwidth) * (box$xmax - box$xmin)/2
237-
middle$x <- middle$x + notchindent
238-
middle$xend <- middle$xend - notchindent
239-
box <- data.frame(
240-
x = c(
241-
box$xmin,
242-
box$xmin,
243-
box$xmin + notchindent,
244-
box$xmin,
245-
box$xmin,
246-
box$xmax,
247-
box$xmax,
248-
box$xmax - notchindent,
249-
box$xmax,
250-
box$xmax,
251-
box$xmin
252-
), y = c(
253-
box$ymax,
254-
box$ynotchupper,
255-
box$y,
256-
box$ynotchlower,
257-
box$ymin,
258-
box$ymin,
259-
box$ynotchlower,
260-
box$y,
261-
box$ynotchupper,
262-
box$ymax,
263-
box$ymax
264-
),
265-
alpha = box$alpha,
266-
colour = box$colour,
267-
size = box$size,
268-
linetype = box$linetype,
269-
fill = box$fill,
270-
group = seq_len(nrow(box)),
271-
stringsAsFactors = FALSE
272-
)
273-
}
274-
275223
# place an invisible marker at the boxplot middle
276224
# for some sensible hovertext
277225
hover_pts <- data %>%
@@ -291,9 +239,48 @@ to_basic.GeomBoxplot <- function(data, prestats_data, layout, params, p, ...) {
291239
) %>%
292240
dplyr::select(x, y = middle, hovertext, alpha, colour)
293241

294-
# to_basic.GeomCrossbar() returns list of 2 data frames
242+
# If boxplot has notches, it needs to drawn as a polygon (instead of a crossbar/rect)
243+
# This code is adapted from GeomCrossbar$draw_panel()
244+
box_dat <- if (!params$notch) {
245+
to_basic.GeomCrossbar(box, params = params)
246+
} else {
247+
# fatten is a parameter to GeomCrossbar$draw_panel() and is always 2 when called from GeomBoxplot$draw_panel()
248+
fatten <- 2
249+
middle <- transform(
250+
box, x = xmin, xend = xmax, yend = y,
251+
size = size * fatten, alpha = NA
252+
)
253+
if (box$ynotchlower < box$ymin || box$ynotchupper > box$ymax)
254+
message("notch went outside hinges. Try setting notch=FALSE.")
255+
notchindent <- (1 - box$notchwidth) * (box$xmax - box$xmin)/2
256+
middle$x <- middle$x + notchindent
257+
middle$xend <- middle$xend - notchindent
258+
259+
box$notchindent <- notchindent
260+
boxes <- split(box, seq_len(nrow(box)))
261+
box <- dplyr::bind_rows(lapply(boxes, function(b) {
262+
dplyr::bind_rows(
263+
dplyr::mutate(b, x = xmin, y = ymax),
264+
dplyr::mutate(b, x = xmin, y = notchupper),
265+
dplyr::mutate(b, x = xmin + notchindent, y = middle),
266+
dplyr::mutate(b, x = xmin, y = notchlower),
267+
dplyr::mutate(b, x = xmin, y = ymin),
268+
dplyr::mutate(b, x = xmax, y = ymin),
269+
dplyr::mutate(b, x = xmax, y = notchlower),
270+
dplyr::mutate(b, x = xmax - notchindent, y = middle),
271+
dplyr::mutate(b, x = xmax, y = notchupper),
272+
dplyr::mutate(b, x = xmax, y = ymax)
273+
)
274+
}))
275+
276+
list(
277+
prefix_class(box, "GeomPolygon"),
278+
to_basic.GeomSegment(middle)
279+
)
280+
}
281+
# box_dat is list of 2 data frames
295282
c(
296-
if (params$notch) list(prefix_class(box, "GeomPolygon")) else to_basic.GeomCrossbar(box, params = params),
283+
box_dat,
297284
list(to_basic.GeomSegment(whiskers)),
298285
list(prefix_class(hover_pts, "GeomPoint")),
299286
if (length(outliers)) list(prefix_class(outliers, "GeomPoint"))

0 commit comments

Comments
 (0)