@@ -178,6 +178,7 @@ GuideLegend <- ggproto(
178
178
key = " legend.key" ,
179
179
key_height = " legend.key.height" ,
180
180
key_width = " legend.key.width" ,
181
+ key_just = " legend.key.justification" ,
181
182
text = " legend.text" ,
182
183
theme.title = " legend.title" ,
183
184
spacing_x = " legend.key.spacing.x" ,
@@ -275,7 +276,6 @@ GuideLegend <- ggproto(
275
276
c(" horizontal" , " vertical" ), arg_nm = " direction"
276
277
)
277
278
params $ n_breaks <- n_breaks <- nrow(params $ key )
278
- params $ n_key_layers <- length(params $ decor ) + 1 # +1 is key background
279
279
280
280
# Resolve shape
281
281
if (! is.null(params $ nrow ) && ! is.null(params $ ncol ) &&
@@ -378,6 +378,9 @@ GuideLegend <- ggproto(
378
378
elements $ key <-
379
379
ggname(" legend.key" , element_grob(elements $ key ))
380
380
}
381
+ if (! is.null(elements $ key_just )) {
382
+ elements $ key_just <- valid.just(elements $ key_just )
383
+ }
381
384
382
385
elements $ text <-
383
386
label_angle_heuristic(elements $ text , elements $ text_position , params $ angle )
@@ -391,22 +394,39 @@ GuideLegend <- ggproto(
391
394
392
395
build_decor = function (decor , grobs , elements , params ) {
393
396
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())
405
406
}
407
+ key <- dec $ draw_key(data , dec $ params , key_size * 10 )
408
+ set_key_size(key , data $ linewidth , data $ size , key_size )
406
409
})
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
410
430
},
411
431
412
432
build_labels = function (key , elements , params ) {
@@ -795,3 +815,7 @@ deprecated_guide_args <- function(
795
815
}
796
816
theme
797
817
}
818
+
819
+ get_attr <- function (x , which , exact = TRUE , default = NULL ) {
820
+ attr(x , which = which , exact = exact ) %|| % default
821
+ }
0 commit comments