Skip to content

Arrow key size #5565

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
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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)

* Legend keys that can draw arrows have their size adjusted for arrows.

* Contour functions will not fail when `options("OutDec")` is not `.` (@eliocamp, #5555).

* The `legend.key` theme element is set to inherit from the `panel.background`
Expand Down
36 changes: 29 additions & 7 deletions R/legend-draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ draw_key_path <- function(data, params, size) {
} else {
data$linetype[is.na(data$linetype)] <- 0
}

segmentsGrob(0.1, 0.5, 0.9, 0.5,
grob <- segmentsGrob(0.1, 0.5, 0.9, 0.5,
gp = gpar(
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
fill = alpha(params$arrow.fill %||% data$colour
Expand All @@ -172,12 +171,19 @@ draw_key_path <- function(data, params, size) {
),
arrow = params$arrow
)
if (!is.null(params$arrow)) {
angle <- deg2rad(params$arrow$angle)
length <- convertUnit(params$arrow$length, "cm", valueOnly = TRUE)
attr(grob, "width") <- cos(angle) * length * 1.25
attr(grob, "height") <- sin(angle) * length * 2
}
grob
}

#' @export
#' @rdname draw_key
draw_key_vpath <- function(data, params, size) {
segmentsGrob(0.5, 0.1, 0.5, 0.9,
grob <- segmentsGrob(0.5, 0.1, 0.5, 0.9,
gp = gpar(
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
lwd = (data$linewidth %||% 0.5) * .pt,
Expand All @@ -186,6 +192,13 @@ draw_key_vpath <- function(data, params, size) {
),
arrow = params$arrow
)
if (!is.null(params$arrow)) {
angle <- deg2rad(params$arrow$angle)
length <- convertUnit(params$arrow$length, "cm", valueOnly = TRUE)
attr(grob, "width") <- sin(angle) * length * 2
attr(grob, "height") <- cos(angle) * length * 1.25
}
grob
}

#' @export
Expand Down Expand Up @@ -215,10 +228,14 @@ draw_key_linerange <- function(data, params, size) {
#' @export
#' @rdname draw_key
draw_key_pointrange <- function(data, params, size) {
grobTree(
draw_key_linerange(data, params, size),
linerange <- draw_key_linerange(data, params, size)
grob <- grobTree(
linerange,
draw_key_point(transform(data, size = (data$size %||% 1.5) * 4), params)
)
attr(grob, "width") <- attr(linerange, "width")
attr(grob, "height") <- attr(linerange, "height")
grob
}

#' @export
Expand All @@ -227,10 +244,15 @@ draw_key_smooth <- function(data, params, size) {
data$fill <- alpha(data$fill %||% "grey60", data$alpha)
data$alpha <- 1

grobTree(
path <- draw_key_path(data, params, size)

grob <- grobTree(
if (isTRUE(params$se)) rectGrob(gp = gpar(col = NA, fill = data$fill)),
draw_key_path(data, params, size)
path
)
attr(grob, "width") <- attr(path, "width")
attr(grob, "height") <- attr(path, "height")
grob
}

#' @export
Expand Down