From 98bb2f26cc8a68e6953bdd5f689107b38567c42b Mon Sep 17 00:00:00 2001 From: Ilia Kats Date: Sun, 8 Dec 2019 20:27:25 +0100 Subject: [PATCH] fix legend justification for fixed aspect ratio plots --- R/plot_patchwork.R | 40 +++- .../add-base-graphics-p1-plot-1-10-1-10.svg | 82 ++++---- tests/figs/far-legend-justification.svg | 193 ++++++++++++++++++ tests/testthat/test-layout.R | 7 + 4 files changed, 273 insertions(+), 49 deletions(-) create mode 100644 tests/figs/far-legend-justification.svg diff --git a/R/plot_patchwork.R b/R/plot_patchwork.R index 483b264..78ae39e 100644 --- a/R/plot_patchwork.R +++ b/R/plot_patchwork.R @@ -392,32 +392,56 @@ simplify_fixed <- function(gt, gt_new, panels, rows, cols) { for (i in seq_len(left - 1)) { table <- gt[p_rows, i] if (length(table$grobs) != 0) { - grobname <- paste(table$layout$name, collapse = ', ') - gt_new <- gtable_add_grob(gt_new, table, rows[1], i, clip = 'off', name = grobname, z = max(table$layout$z)) + if (length(table$grobs) == 1) { + grobname <- table$layout$name + grob <- table$grobs[[1]] + } else { + grobname <- paste(table$layout$name, collapse = ', ') + grob <- table + } + gt_new <- gtable_add_grob(gt_new, grob, rows[1], i, clip = 'off', name = grobname, z = max(table$layout$z)) } } right <- if (length(right) != 0) max(right) else cols[2] for (i in seq_len(ncol(gt) - right)) { table <- gt[p_rows, i + right] if (length(table$grobs) != 0) { - grobname <- paste(table$layout$name, collapse = ', ') - gt_new <- gtable_add_grob(gt_new, table, rows[1], i + cols[1] + right - cols[2], clip = 'off', name = grobname, z = max(table$layout$z)) + if (length(table$grobs) == 1) { + grobname <- table$layout$name + grob <- table$grobs[[1]] + } else { + grobname <- paste(table$layout$name, collapse = ', ') + grob <- table + } + gt_new <- gtable_add_grob(gt_new, grob, rows[1], i + cols[1] + right - cols[2], clip = 'off', name = grobname, z = max(table$layout$z)) } } top <- if (length(top) != 0) min(top) else rows[1] for (i in seq_len(top - 1)) { table <- gt[i, p_cols] if (length(table$grobs) != 0) { - grobname <- paste(table$layout$name, collapse = ', ') - gt_new <- gtable_add_grob(gt_new, table, i, cols[1], clip = 'off', name = grobname, z = max(table$layout$z)) + if (length(table$grobs) == 1) { + grobname <- table$layout$name + grob <- table$grobs[[1]] + } else { + grobname <- paste(table$layout$name, collapse = ', ') + grob <- table + } + gt_new <- gtable_add_grob(gt_new, grob, i, cols[1], clip = 'off', name = grobname, z = max(table$layout$z)) } } bottom <- if (length(bottom) != 0) max(bottom) else rows[2] for (i in seq_len(nrow(gt) - bottom)) { table <- gt[i + bottom, p_cols] if (length(table$grobs) != 0) { - grobname <- paste(table$layout$name, collapse = ', ') - gt_new <- gtable_add_grob(gt_new, table, i + rows[1] + bottom - rows[2], cols[1], clip = 'off', name = grobname, z = max(table$layout$z)) + if (length(table$grobs) == 1) { + grobname <- table$layout$name + grob <- table$grobs[[1]] + } else { + grobname <- paste(table$layout$name, collapse = ', ') + grob <- table + } + gt_new <- gtable_add_grob(gt_new, grob, i + rows[1] + bottom - rows[2], cols[1], clip = 'off', name = grobname, z = max(table$layout$z)) } } panel_name <- paste0('panel; ', paste(panels$layout$name, collapse = ', ')) diff --git a/tests/figs/add-base-graphics-p1-plot-1-10-1-10.svg b/tests/figs/add-base-graphics-p1-plot-1-10-1-10.svg index 08cfca0..da6f657 100644 --- a/tests/figs/add-base-graphics-p1-plot-1-10-1-10.svg +++ b/tests/figs/add-base-graphics-p1-plot-1-10-1-10.svg @@ -118,8 +118,8 @@ - - + + @@ -128,8 +128,8 @@ - - + + @@ -139,50 +139,50 @@ - - + + - - - - - - - - - - + + + + + + + + + + - - - - - - -2 -4 -6 -8 -10 - - - - - - -2 -4 -6 -8 -10 - -1:10 -1:10 + + + + + + +2 +4 +6 +8 +10 + + + + + + +2 +4 +6 +8 +10 + +1:10 +1:10 diff --git a/tests/figs/far-legend-justification.svg b/tests/figs/far-legend-justification.svg new file mode 100644 index 0000000..7230e29 --- /dev/null +++ b/tests/figs/far-legend-justification.svg @@ -0,0 +1,193 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +16 +18 +20 +22 + + + + +qsec + + + + + +4 +5 +6 +7 +8 +cyl + +as.factor(vs) + + + + +0 +1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +16 +18 +20 +22 + + + + +qsec + + + + + +4 +5 +6 +7 +8 +cyl + +a very looooooong legend title + + + + +0 +1 +FAR legend justification + diff --git a/tests/testthat/test-layout.R b/tests/testthat/test-layout.R index c48aeba..f468e8e 100644 --- a/tests/testthat/test-layout.R +++ b/tests/testthat/test-layout.R @@ -45,4 +45,11 @@ test_that("Fixed aspect plots behave", { expect_doppelganger('FAR dimensions can be set with units:...', { p1 + p2 + p_f + plot_layout(widths = unit(c(1, 3, -1), c('null', 'cm', 'null'))) }) + + p_l1 <- ggplot(mtcars, aes(cyl, qsec, color=as.factor(vs))) + + geom_point() + p_l2 <- p_l1 + labs(color="a very looooooong legend title") + expect_doppelganger('FAR legend justification', { + p_l1 + p_l2 + plot_layout(ncol=1) & theme(legend.justification = "left", aspect.ratio=1) + }) })