@@ -653,11 +653,14 @@ Guides <- ggproto(
653
653
height = heightDetails(grobs [[i ]]))
654
654
)
655
655
}
656
-
657
- spacing <- convertWidth(theme $ legend.spacing.x , " cm" )
656
+ spacing <- theme $ legend.spacing.x
657
+ stretch_spacing <- any(unitType(spacing ) == " null" )
658
+ if (! stretch_spacing ) {
659
+ spacing <- convertWidth(spacing , " cm" )
660
+ }
658
661
heights <- unit(height_cm(lapply(heights , sum )), " cm" )
659
662
660
- if (stretch_x ) {
663
+ if (stretch_x || stretch_spacing ) {
661
664
widths <- redistribute_null_units(widths , spacing , margin , " width" )
662
665
vp_width <- unit(1 , " npc" )
663
666
} else {
@@ -692,10 +695,14 @@ Guides <- ggproto(
692
695
)
693
696
}
694
697
695
- spacing <- convertHeight(theme $ legend.spacing.y , " cm" )
698
+ spacing <- theme $ legend.spacing.y
699
+ stretch_spacing <- any(unitType(spacing ) == " null" )
700
+ if (! stretch_spacing ) {
701
+ spacing <- convertWidth(spacing , " cm" )
702
+ }
696
703
widths <- unit(width_cm(lapply(widths , sum )), " cm" )
697
704
698
- if (stretch_y ) {
705
+ if (stretch_y || stretch_spacing ) {
699
706
heights <- redistribute_null_units(heights , spacing , margin , " height" )
700
707
vp_height <- unit(1 , " npc" )
701
708
} else {
@@ -735,10 +742,10 @@ Guides <- ggproto(
735
742
)
736
743
737
744
# Set global margin
738
- if (stretch_x ) {
745
+ if (stretch_x || stretch_spacing ) {
739
746
global_margin [c(2 , 4 )] <- unit(0 , " cm" )
740
747
}
741
- if (stretch_y ) {
748
+ if (stretch_y || stretch_spacing ) {
742
749
global_margin [c(1 , 3 )] <- unit(0 , " cm" )
743
750
}
744
751
guides <- gtable_add_padding(guides , global_margin )
@@ -933,26 +940,20 @@ redistribute_null_units <- function(units, spacing, margin, type = "width") {
933
940
}
934
941
935
942
# Get spacing between guides and margins in absolute units
936
- size <- switch (type , width = convertWidth , height = convertHeight )
937
- spacing <- size(spacing , " cm" , valueOnly = TRUE )
938
- spacing <- sum(rep(spacing , length(units ) - 1 ))
943
+ size <- switch (type , width = width_cm , height = height_cm )
944
+ spacing <- sum(rep(spacing , length.out = length(units ) - 1 ))
939
945
margin <- switch (type , width = margin [c(2 , 4 )], height = margin [c(1 , 3 )])
940
- margin <- sum(size(margin , " cm " , valueOnly = TRUE ))
946
+ margin <- sum(size(margin ))
941
947
942
948
# Get the absolute parts of the unit
943
- absolute <- vapply(units , function (u ) {
944
- u <- absolute.size(u )
945
- u <- size(u , " cm" , valueOnly = TRUE )
946
- sum(u )
947
- }, numeric (1 ))
948
- absolute_sum <- sum(absolute ) + spacing + margin
949
+ absolute <- vapply(units , function (u ) sum(size(absolute.size(u ))), numeric (1 ))
950
+ absolute_sum <- sum(absolute ) + sum(size(spacing )) + margin
949
951
950
952
# Get the null parts of the unit
953
+ num_null <- function (x ) sum(as.numeric(x )[unitType(x ) == " null" ])
951
954
relative <- rep(0 , length(units ))
952
- relative [has_null ] <- vapply(units [has_null ], function (u ) {
953
- sum(as.numeric(u )[unitType(u ) == " null" ])
954
- }, numeric (1 ))
955
- relative_sum <- sum(relative )
955
+ relative [has_null ] <- vapply(units [has_null ], num_null , numeric (1 ))
956
+ relative_sum <- sum(relative ) + num_null(spacing )
956
957
957
958
if (relative_sum == 0 ) {
958
959
return (unit(absolute , " cm" ))
0 commit comments