From e51ca467eaec2268e298e14068af1f11ee8a484e Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:23:46 +0100 Subject: [PATCH] Size of text/label keys (#5562) * improve text label * improve label labels * add news bullet --- NEWS.md | 2 ++ R/legend-draw.R | 64 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/NEWS.md b/NEWS.md index 476076f956..0b05e50440 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `draw_key_label()` now better reflects the appearance of labels. + * The `minor_breaks` function argument in scales can now take a function with two arguments: the scale's limits and the scale's major breaks (#3583). diff --git a/R/legend-draw.R b/R/legend-draw.R index e039e97ac3..8ee116f65c 100644 --- a/R/legend-draw.R +++ b/R/legend-draw.R @@ -236,26 +236,68 @@ draw_key_smooth <- function(data, params, size) { #' @export #' @rdname draw_key draw_key_text <- function(data, params, size) { - if(is.null(data$label)) data$label <- "a" - - textGrob(data$label, 0.5, 0.5, - rot = data$angle %||% 0, + data$label <- data$label %||% "a" + just <- rotate_just(data$angle, data$hjust, data$vjust) + grob <- titleGrob( + data$label, + x = unit(just$hjust, "npc"), y = unit(just$vjust, "npc"), + angle = data$angle, + hjust = data$hjust, + vjust = data$vjust, gp = gpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), - fontfamily = data$family %||% "", - fontface = data$fontface %||% 1, - fontsize = (data$size %||% 3.88) * .pt - ) + fontfamily = data$family %||% "", + fontface = data$fontface %||% 1, + fontsize = (data$size %||% 3.88) * .pt + ), + margin = margin(0.1, 0.1, 0.1, 0.1, unit = "lines"), + margin_x = TRUE, margin_y = TRUE ) + attr(grob, "width") <- convertWidth(grobWidth(grob), "cm", valueOnly = TRUE) + attr(grob, "height") <- convertHeight(grobHeight(grob), "cm", valueOnly = TRUE) + grob } #' @export #' @rdname draw_key draw_key_label <- function(data, params, size) { - grobTree( - draw_key_rect(data, list()), - draw_key_text(data, list()) + data$label <- data$label %||% "a" + just <- rotate_just(data$angle, data$hjust, data$vjust) + padding <- rep(params$label.padding, length.out = 4) + descent <- font_descent( + family = data$family %||% "", + face = data$fontface %||% 1, + size = data$size %||% 3.88 + ) + grob <- labelGrob( + data$label, + x = unit(just$hjust, "npc"), + y = unit(just$vjust, "npc") + descent, + angle = data$angle, + just = c(data$hjust, data$vjust), + padding = padding, + r = params$label.r, + text.gp = gpar( + col = data$colour %||% "black", + fontfamily = data$family %||% "", + fontface = data$fontface %||% 1, + fontsize = (data$size %||% 3.88) * .pt + ), + rect.gp = gpar( + col = if (isTRUE(all.equal(params$label.size, 0))) NA else data$colour, + fill = alpha(data$fill %||% "white", data$alpha), + lwd = params$label.size * .pt + ) ) + angle <- deg2rad(data$angle %||% 0) + text <- grob$children[[2]] + width <- convertWidth(grobWidth(text), "cm", valueOnly = TRUE) + height <- convertHeight(grobHeight(text), "cm", valueOnly = TRUE) + x <- c(0, 0, width, width) + y <- c(0, height, height, 0) + attr(grob, "width") <- diff(range(x * cos(angle) - y * sin(angle))) + attr(grob, "height") <- diff(range(x * sin(angle) + y * cos(angle))) + grob } #' @export