@@ -94,6 +94,7 @@ Layout <- ggproto("Layout", NULL,
9494 theme ,
9595 self $ facet_params
9696 )
97+ plot_table <- self $ set_panel_size(plot_table , theme )
9798
9899 # Draw individual labels, then add to gtable
99100 labels <- self $ coord $ labels(
@@ -297,10 +298,48 @@ Layout <- ggproto("Layout", NULL,
297298 })
298299 names(label_grobs ) <- names(labels )
299300 label_grobs
301+ },
302+
303+ set_panel_size = function (table , theme ) {
304+
305+ new_widths <- calc_element(" panel.widths" , theme )
306+ new_heights <- calc_element(" panel.heights" , theme )
307+
308+ if (is.null(new_widths ) && is.null(new_heights )) {
309+ return (table )
310+ }
311+
312+ rows <- panel_rows(table )
313+ cols <- panel_cols(table )
314+
315+ if (length(new_widths ) == 1L && nrow(cols ) > 1L ) {
316+ # Get total size of non-panel widths in between panels
317+ extra <- setdiff(seq(min(cols $ l ), max(cols $ r )), union(cols $ l , cols $ r ))
318+ extra <- unit(sum(width_cm(table $ widths [extra ])), " cm" )
319+ # Distribute width proportionally
320+ relative <- as.numeric(table $ widths [cols $ l ]) # assumed to be simple units
321+ new_widths <- (new_widths - extra ) * (relative / sum(relative ))
322+ }
323+ if (! is.null(new_widths )) {
324+ table $ widths [cols $ l ] <- rep(new_widths , length.out = nrow(cols ))
325+ }
326+
327+ if (length(new_heights ) == 1L && nrow(rows ) > 1L ) {
328+ # Get total size of non-panel heights in between panels
329+ extra <- setdiff(seq(min(rows $ t ), max(rows $ t )), union(rows $ t , rows $ b ))
330+ extra <- unit(sum(height_cm(table $ heights [extra ])), " cm" )
331+ # Distribute height proportionally
332+ relative <- as.numeric(table $ heights [rows $ t ]) # assumed to be simple units
333+ new_heights <- (new_heights - extra ) * (relative / sum(relative ))
334+ }
335+ if (! is.null(new_heights )) {
336+ table $ heights [rows $ t ] <- rep(new_heights , length.out = nrow(rows ))
337+ }
338+
339+ table
300340 }
301341)
302342
303-
304343# Helpers -----------------------------------------------------------------
305344
306345# Function for applying scale method to multiple variables in a given
0 commit comments