Skip to content

Commit 0ee63c3

Browse files
authored
Text key justification (#5618)
* `compute_just()` returns non-characters as-is * `rotate_just()` handles character justification * better text legend justification
1 parent 47b4bd0 commit 0ee63c3

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

R/geom-label.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,8 @@ GeomLabel <- ggproto("GeomLabel", Geom,
7373
}
7474

7575
data <- coord$transform(data, panel_params)
76-
if (is.character(data$vjust)) {
77-
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
78-
}
79-
if (is.character(data$hjust)) {
80-
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
81-
}
76+
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
77+
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
8278
if (!inherits(label.padding, "margin")) {
8379
label.padding <- rep(label.padding, length.out = 4)
8480
}

R/geom-text.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,8 @@ GeomText <- ggproto("GeomText", Geom,
220220

221221
data <- coord$transform(data, panel_params)
222222

223-
if (is.character(data$vjust)) {
224-
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
225-
}
226-
if (is.character(data$hjust)) {
227-
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
228-
}
223+
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
224+
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
229225

230226
size.unit <- resolve_text_unit(size.unit)
231227

@@ -248,7 +244,10 @@ GeomText <- ggproto("GeomText", Geom,
248244
draw_key = draw_key_text
249245
)
250246

251-
compute_just <- function(just, a, b = a, angle = 0) {
247+
compute_just <- function(just, a = 0.5, b = a, angle = 0) {
248+
if (!is.character(just)) {
249+
return(just)
250+
}
252251
# As justification direction is relative to the text, not the plotting area
253252
# we need to swap x and y if text direction is rotated so that hjust is
254253
# applied along y and vjust along x.

R/legend-draw.R

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,16 @@ draw_key_smooth <- function(data, params, size) {
258258
#' @export
259259
#' @rdname draw_key
260260
draw_key_text <- function(data, params, size) {
261-
data <- replace_null(
262-
unclass(data),
263-
label = "a", hjust = 0.5, vjust = 0.5, angle = 0
264-
)
265-
just <- rotate_just(data$angle, data$hjust, data$vjust)
266-
grob <- titleGrob(
261+
data <- replace_null(unclass(data), label = "a", angle = 0)
262+
hjust <- compute_just(data$hjust %||% 0.5)
263+
vjust <- compute_just(data$vjust %||% 0.5)
264+
just <- rotate_just(data$angle, hjust, vjust)
265+
grob <- titleGrob(
267266
data$label,
268267
x = unit(just$hjust, "npc"), y = unit(just$vjust, "npc"),
269268
angle = data$angle,
270-
hjust = data$hjust,
271-
vjust = data$vjust,
269+
hjust = hjust,
270+
vjust = vjust,
272271
gp = gpar(
273272
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
274273
fontfamily = data$family %||% "",
@@ -286,12 +285,11 @@ draw_key_text <- function(data, params, size) {
286285
#' @export
287286
#' @rdname draw_key
288287
draw_key_label <- function(data, params, size) {
289-
data <- replace_null(
290-
unclass(data),
291-
label = "a", hjust = 0.5, vjust = 0.5, angle = 0
292-
)
288+
data <- replace_null(unclass(data), label = "a", angle = 0)
293289
params$label.size <- params$label.size %||% 0.25
294-
just <- rotate_just(data$angle, data$hjust, data$vjust)
290+
hjust <- compute_just(data$hjust %||% 0.5)
291+
vjust <- compute_just(data$vjust %||% 0.5)
292+
just <- rotate_just(data$angle, hjust, vjust)
295293
padding <- rep(params$label.padding %||% unit(0.25, "lines"), length.out = 4)
296294
descent <- font_descent(
297295
family = data$family %||% "",
@@ -303,7 +301,7 @@ draw_key_label <- function(data, params, size) {
303301
x = unit(just$hjust, "npc"),
304302
y = unit(just$vjust, "npc") + descent,
305303
angle = data$angle,
306-
just = c(data$hjust, data$vjust),
304+
just = c(hjust, vjust),
307305
padding = padding,
308306
r = params$label.r %||% unit(0.15, "lines"),
309307
text.gp = gpar(

R/margins.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@ rotate_just <- function(angle, hjust, vjust) {
253253

254254
angle <- (angle %||% 0) %% 360
255255

256+
if (is.character(hjust)) {
257+
hjust <- match(hjust, c("left", "right")) - 1
258+
hjust[is.na(hjust)] <- 0.5
259+
}
260+
if (is.character(vjust)) {
261+
vjust <- match(vjust, c("bottom", "top")) - 1
262+
vjust[is.na(vjust)] <- 0.5
263+
}
264+
256265
# Apply recycle rules
257266
size <- vec_size_common(angle, hjust, vjust)
258267
angle <- vec_recycle(angle, size)

0 commit comments

Comments
 (0)