From e64f34a1fe2cf58cb2a0b37844cbb105b99159e8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 25 Jan 2024 12:49:03 +0100 Subject: [PATCH 1/3] balance labels --- R/geom-label.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/geom-label.R b/R/geom-label.R index d83434b386..31b65fcacb 100644 --- a/R/geom-label.R +++ b/R/geom-label.R @@ -143,6 +143,11 @@ labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), descent <- font_descent( text.gp$fontfamily, text.gp$fontface, text.gp$fontsize, text.gp$cex ) + # To balance labels, we ensure the top includes at least the descent height + # and subtract the descent height from the bottom padding + padding[1] <- unit.pmax(padding[1], descent) + padding[3] <- unit.pmax(padding[3] - descent, unit(0, "pt")) + hjust <- resolveHJust(just, NULL) vjust <- resolveVJust(just, NULL) From f0a0a7cf97cc794b5decbce006db2092ffeb04d0 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 25 Jan 2024 14:53:28 +0100 Subject: [PATCH 2/3] get `vjust` right --- R/geom-label.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/R/geom-label.R b/R/geom-label.R index 31b65fcacb..c55ed6a332 100644 --- a/R/geom-label.R +++ b/R/geom-label.R @@ -152,16 +152,18 @@ labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), vjust <- resolveVJust(just, NULL) text <- titleGrob( - label = label, hjust = hjust, vjust = vjust, x = x, y = y, + label = label, hjust = hjust, vjust = vjust, x = x, + y = y + (1 - vjust) * descent, margin = padding, margin_x = TRUE, margin_y = TRUE, gp = text.gp ) + height <- heightDetails(text) box <- roundrectGrob( - x = x, y = y - (1 - vjust) * descent, + x = x, y = y + (0.5 - vjust) * height, width = widthDetails(text), - height = heightDetails(text), - just = c(hjust, vjust), + height = height, + just = c(hjust, 0.5), r = r, gp = rect.gp, name = "box" ) From e96a70adfda5768a41c4b6c693a635c0d4e737a2 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 25 Jan 2024 14:53:34 +0100 Subject: [PATCH 3/3] update snapshot --- tests/testthat/_snaps/geom-sf/labels-for-north-carolina.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/_snaps/geom-sf/labels-for-north-carolina.svg b/tests/testthat/_snaps/geom-sf/labels-for-north-carolina.svg index ab6663e489..cf5ffdbbf2 100644 --- a/tests/testthat/_snaps/geom-sf/labels-for-north-carolina.svg +++ b/tests/testthat/_snaps/geom-sf/labels-for-north-carolina.svg @@ -27,7 +27,7 @@ - + ashe