Skip to content

Flipped crossbar and boxplot keys #5083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Fixed misbehaviour of `draw_key_boxplot()` and `draw_key_crossbar()` with
skewed key aspect ratio (@teunbrand, #5082).
* `scale_*_binned()` handles zero-range limits more gracefully (@teunbrand,
#5066)
* Binned scales are now compatible with `trans = "date"` and `trans = "time"`
Expand Down
71 changes: 45 additions & 26 deletions R/legend-draw.r
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,58 @@ draw_key_blank <- function(data, params, size) {
#' @export
#' @rdname draw_key
draw_key_boxplot <- function(data, params, size) {
grobTree(
linesGrob(0.5, c(0.1, 0.25)),
linesGrob(0.5, c(0.75, 0.9)),
rectGrob(height = 0.5, width = 0.75),
linesGrob(c(0.125, 0.875), 0.5),
gp = gpar(
col = data$colour %||% "grey20",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = (data$linewidth %||% 0.5) * .pt,
lty = data$linetype %||% 1,
lineend = params$lineend %||% "butt",
linejoin = params$linejoin %||% "mitre"
),
vp = if (isTRUE(params$flipped_aes)) viewport(angle = -90)
gp <- gpar(
col = data$colour %||% "grey20",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = (data$linewidth %||% 0.5) * .pt,
lty = data$linetype %||% 1,
lineend = params$lineend %||% "butt",
linejoin = params$linejoin %||% "mitre"
)

if (isTRUE(params$flipped_aes)) {
grobTree(
linesGrob(c(0.1, 0.25), 0.5),
linesGrob(c(0.75, 0.9), 0.5),
rectGrob(width = 0.5, height = 0.75),
linesGrob(0.5, c(0.125, 0.875)),
gp = gp
)
} else {
grobTree(
linesGrob(0.5, c(0.1, 0.25)),
linesGrob(0.5, c(0.75, 0.9)),
rectGrob(height = 0.5, width = 0.75),
linesGrob(c(0.125, 0.875), 0.5),
gp = gp
)
}
}

#' @export
#' @rdname draw_key
draw_key_crossbar <- function(data, params, size) {
grobTree(
rectGrob(height = 0.5, width = 0.75),
linesGrob(c(0.125, 0.875), 0.5),
gp = gpar(
col = data$colour %||% "grey20",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = (data$linewidth %||% 0.5) * .pt,
lty = data$linetype %||% 1,
lineend = params$lineend %||% "butt",
linejoin = params$linejoin %||% "mitre"
),
vp = if (isTRUE(params$flipped_aes)) viewport(angle = -90)
gp <- gpar(
col = data$colour %||% "grey20",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = (data$linewidth %||% 0.5) * .pt,
lty = data$linetype %||% 1,
lineend = params$lineend %||% "butt",
linejoin = params$linejoin %||% "mitre"
)
if (isTRUE(params$flipped_aes)) {
grobTree(
rectGrob(height = 0.75, width = 0.5),
linesGrob(0.5, c(0.125, 0.875)),
gp = gp
)
} else {
grobTree(
rectGrob(height = 0.5, width = 0.75),
linesGrob(c(0.125, 0.875), 0.5),
gp = gp
)
}
}

#' @export
Expand Down
20 changes: 8 additions & 12 deletions tests/testthat/_snaps/draw-key/horizontal-boxplot-and-crossbar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.