Skip to content

Commit 87abade

Browse files
authored
Balance geom label whitespace (#5663)
* balance labels * get `vjust` right * update snapshot
1 parent 3d5e9d2 commit 87abade

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

R/geom-label.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,27 @@ labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"),
139139
descent <- font_descent(
140140
text.gp$fontfamily, text.gp$fontface, text.gp$fontsize, text.gp$cex
141141
)
142+
# To balance labels, we ensure the top includes at least the descent height
143+
# and subtract the descent height from the bottom padding
144+
padding[1] <- unit.pmax(padding[1], descent)
145+
padding[3] <- unit.pmax(padding[3] - descent, unit(0, "pt"))
146+
142147
hjust <- resolveHJust(just, NULL)
143148
vjust <- resolveVJust(just, NULL)
144149

145150
text <- titleGrob(
146-
label = label, hjust = hjust, vjust = vjust, x = x, y = y,
151+
label = label, hjust = hjust, vjust = vjust, x = x,
152+
y = y + (1 - vjust) * descent,
147153
margin = padding, margin_x = TRUE, margin_y = TRUE,
148154
gp = text.gp
149155
)
150156

157+
height <- heightDetails(text)
151158
box <- roundrectGrob(
152-
x = x, y = y - (1 - vjust) * descent,
159+
x = x, y = y + (0.5 - vjust) * height,
153160
width = widthDetails(text),
154-
height = heightDetails(text),
155-
just = c(hjust, vjust),
161+
height = height,
162+
just = c(hjust, 0.5),
156163
r = r, gp = rect.gp, name = "box"
157164
)
158165

tests/testthat/_snaps/geom-sf/labels-for-north-carolina.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)