Skip to content

Commit 627c416

Browse files
authored
Fix problem with arrow lengths in glyph drawing (#6595)
* prevent error with vectorised arrows * add arrow logic to other line-ish keys * add news bullet
1 parent c0a6923 commit 627c416

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* `stat_boxplot()` treats `width` as an optional aesthetic (@Yunuuuu, #6575)
1919
* Fixed regression where the first (unnamed) argument to colour/fill scales was
2020
not passed as the `name` argument (@teunbrand, #6623)
21+
* Fixed issue where vectorised `arrow()`s caused errors in drawing the
22+
legend glyphs (@teunbrand, #6594)
2123

2224
# ggplot2 4.0.0
2325

R/legend-draw.R

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ draw_key_abline <- function(data, params, size) {
4444
segmentsGrob(0, 0, 1, 1,
4545
gp = gg_par(
4646
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
47+
fill = alpha(params$arrow.fill %||% data$colour %||% data$fill %||% "black", data$alpha),
4748
lwd = data$linewidth %||% 0.5,
4849
lty = data$linetype %||% 1,
4950
lineend = params$lineend %||% "butt"
50-
)
51+
),
52+
arrow = params[["arrow"]]
5153
)
5254
}
5355

@@ -211,8 +213,10 @@ draw_key_path <- function(data, params, size) {
211213
)
212214
if (!is.null(params[["arrow"]])) {
213215
angle <- deg2rad(params[["arrow"]]$angle)
214-
length <- convertUnit(params[["arrow"]]$length, "cm", valueOnly = TRUE)
216+
length <- convertUnit(params[["arrow"]]$length[1], "cm", valueOnly = TRUE)
217+
# grob spans '0.8 * full width', so multiply by 1/0.8
215218
attr(grob, "width") <- cos(angle) * length * 1.25
219+
# arrow is symmetric, so double height
216220
attr(grob, "height") <- sin(angle) * length * 2
217221
}
218222
grob
@@ -232,8 +236,10 @@ draw_key_vpath <- function(data, params, size) {
232236
)
233237
if (!is.null(params[["arrow"]])) {
234238
angle <- deg2rad(params[["arrow"]]$angle)
235-
length <- convertUnit(params[["arrow"]]$length, "cm", valueOnly = TRUE)
239+
length <- convertUnit(params[["arrow"]]$length[1], "cm", valueOnly = TRUE)
240+
# arrow is symmetric, so double width
236241
attr(grob, "width") <- sin(angle) * length * 2
242+
# grob spans '0.8 * full height', so multiply by 1/0.8
237243
attr(grob, "height") <- cos(angle) * length * 1.25
238244
}
239245
grob
@@ -369,14 +375,26 @@ draw_key_label <- function(data, params, size) {
369375
#' @export
370376
#' @rdname draw_key
371377
draw_key_vline <- function(data, params, size) {
372-
segmentsGrob(0.5, 0, 0.5, 1,
378+
# main difference between `draw_key_vline` and `draw_key_vpath` is that
379+
# `draw_key_vline` spans the whole height
380+
grob <- segmentsGrob(0.5, 0, 0.5, 1,
373381
gp = gg_par(
374382
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
383+
fill = alpha(params$arrow.fill %||% data$colour %||% data$fill %||% "black", data$alpha),
375384
lwd = data$linewidth %||% 0.5,
376385
lty = data$linetype %||% 1,
377386
lineend = params$lineend %||% "butt"
378-
)
387+
),
388+
arrow = params[["arrow"]]
379389
)
390+
if (!is.null(params[["arrow"]])) {
391+
angle <- deg2rad(params[["arrow"]]$angle)
392+
length <- convertUnit(params[["arrow"]]$length[1], "cm", valueOnly = TRUE)
393+
# arrow is symmetric, so use double the width
394+
attr(grob, "width") <- sin(angle) * length * 2
395+
attr(grob, "height") <- cos(angle) * length
396+
}
397+
grob
380398
}
381399

382400
#' @export
@@ -385,16 +403,17 @@ draw_key_timeseries <- function(data, params, size) {
385403
if (is.null(data$linetype)) {
386404
data$linetype <- 0
387405
}
388-
389406
grid::linesGrob(
390407
x = c(0, 0.4, 0.6, 1),
391408
y = c(0.1, 0.6, 0.4, 0.9),
392409
gp = gg_par(
393410
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
411+
fill = alpha(params$arrow.fill %||% data$colour %||% data$fill %||% "black", data$alpha),
394412
lwd = data$linewidth %||% 0.5,
395413
lty = data$linetype %||% 1,
396414
lineend = params$lineend %||% "butt",
397415
linejoin = params$linejoin %||% "round"
398-
)
416+
),
417+
arrow = params[["arrow"]]
399418
)
400419
}

0 commit comments

Comments
 (0)