diff --git a/R/geom-label.R b/R/geom-label.R index d83434b386..c55ed6a332 100644 --- a/R/geom-label.R +++ b/R/geom-label.R @@ -143,20 +143,27 @@ 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) 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" ) 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