@@ -178,6 +178,7 @@ GuideLegend <- ggproto(
178178 key = "legend.key",
179179 key_height = "legend.key.height",
180180 key_width = "legend.key.width",
181+ key_just = "legend.key.justification",
181182 text = "legend.text",
182183 theme.title = "legend.title",
183184 spacing_x = "legend.key.spacing.x",
@@ -275,7 +276,6 @@ GuideLegend <- ggproto(
275276 c("horizontal", "vertical"), arg_nm = "direction"
276277 )
277278 params$n_breaks <- n_breaks <- nrow(params$key)
278- params$n_key_layers <- length(params$decor) + 1 # +1 is key background
279279
280280 # Resolve shape
281281 if (!is.null(params$nrow) && !is.null(params$ncol) &&
@@ -378,6 +378,9 @@ GuideLegend <- ggproto(
378378 elements$key <-
379379 ggname("legend.key", element_grob(elements$key))
380380 }
381+ if (!is.null(elements$key_just)) {
382+ elements$key_just <- valid.just(elements$key_just)
383+ }
381384
382385 elements$text <-
383386 label_angle_heuristic(elements$text, elements$text_position, params$angle)
@@ -391,22 +394,39 @@ GuideLegend <- ggproto(
391394
392395 build_decor = function(decor, grobs, elements, params) {
393396
394- key_size <- c(elements$width_cm, elements$height_cm) * 10
395-
396- draw <- function(i) {
397- bg <- elements$key
398- keys <- lapply(decor, function(g) {
399- data <- vec_slice(g$data, i)
400- if (data$.draw %||% TRUE) {
401- key <- g$draw_key(data, g$params, key_size)
402- set_key_size(key, data$linewidth, data$size, key_size / 10)
403- } else {
404- zeroGrob()
397+ key_size <- c(elements$width_cm, elements$height_cm)
398+ just <- elements$key_just
399+ idx <- seq_len(params$n_breaks)
400+
401+ key_glyphs <- lapply(idx, function(i) {
402+ glyph <- lapply(decor, function(dec) {
403+ data <- vec_slice(dec$data, i)
404+ if (!(data$.draw %||% TRUE)) {
405+ return(zeroGrob())
405406 }
407+ key <- dec$draw_key(data, dec$params, key_size * 10)
408+ set_key_size(key, data$linewidth, data$size, key_size)
406409 })
407- c(list(bg), keys)
408- }
409- unlist(lapply(seq_len(params$n_breaks), draw), FALSE)
410+
411+ width <- vapply(glyph, get_attr, which = "width", default = 0, numeric(1))
412+ width <- max(width, 0, key_size[1], na.rm = TRUE)
413+ height <- vapply(glyph, get_attr, which = "height", default = 0, numeric(1))
414+ height <- max(height, 0, key_size[2], na.rm = TRUE)
415+
416+ vp <- NULL
417+ if (!is.null(just)) {
418+ vp <- viewport(
419+ x = just[1], y = just[2], just = just,
420+ width = unit(width, "cm"), height = unit(height, "cm")
421+ )
422+ }
423+
424+ grob <- gTree(children = inject(gList(elements$key, !!!glyph)), vp = vp)
425+ attr(grob, "width") <- width
426+ attr(grob, "height") <- height
427+ grob
428+ })
429+ key_glyphs
410430 },
411431
412432 build_labels = function(key, elements, params) {
@@ -795,3 +815,7 @@ deprecated_guide_args <- function(
795815 }
796816 theme
797817}
818+
819+ get_attr <- function(x, which, exact = TRUE, default = NULL) {
820+ attr(x, which = which, exact = exact) %||% default
821+ }
0 commit comments