From 1b133f4fb3afeac0fbcace199218d809ad2394ca Mon Sep 17 00:00:00 2001 From: Doubt-0KB <98461207+Doubt-0KB@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:24:19 +0900 Subject: [PATCH 1/3] Update plot_grid.R Add the function which reoder the labels when byrow=F --- R/plot_grid.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/plot_grid.R b/R/plot_grid.R index 2501762..162afe4 100644 --- a/R/plot_grid.R +++ b/R/plot_grid.R @@ -177,8 +177,10 @@ plot_grid <- function(..., plotlist = NULL, align = c("none", "h", "v", "hv"), if (is.null(cols)) cols <- ceiling(num_plots/rows) if (is.null(rows)) rows <- ceiling(num_plots/cols) - # if the user wants to layout the plots by column, we use the calculated rows to reorder plots - if (!isTRUE(byrow)) plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))] + # if the user wants to layout the plots by column, we use the calculated rows to reorder plots and labels + if (!isTRUE(byrow)) + plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))] + labels <- labels[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))] # Align the plots (if specified) grobs <- align_plots(plotlist = plots, align = align, axis = axis, greedy = greedy) From 2d266f63acc30f2703ab17ec862ababa11bdb4e2 Mon Sep 17 00:00:00 2001 From: Doubt-0KB <98461207+Doubt-0KB@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:31:56 +0900 Subject: [PATCH 2/3] fix code and add tests --- R/plot_grid.R | 3 +- tests/figs/plot-grid/byrow-is-false.svg | 85 +++++++++++++++++++++++++ tests/figs/plot-grid/byrow-is-true.svg | 85 +++++++++++++++++++++++++ tests/testthat/test_plot_grid.R | 15 +++++ 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 tests/figs/plot-grid/byrow-is-false.svg create mode 100644 tests/figs/plot-grid/byrow-is-true.svg diff --git a/R/plot_grid.R b/R/plot_grid.R index 162afe4..064ad00 100644 --- a/R/plot_grid.R +++ b/R/plot_grid.R @@ -178,9 +178,10 @@ plot_grid <- function(..., plotlist = NULL, align = c("none", "h", "v", "hv"), if (is.null(rows)) rows <- ceiling(num_plots/cols) # if the user wants to layout the plots by column, we use the calculated rows to reorder plots and labels - if (!isTRUE(byrow)) + if (!isTRUE(byrow)){ plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))] labels <- labels[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))] + } # Align the plots (if specified) grobs <- align_plots(plotlist = plots, align = align, axis = axis, greedy = greedy) diff --git a/tests/figs/plot-grid/byrow-is-false.svg b/tests/figs/plot-grid/byrow-is-false.svg new file mode 100644 index 0000000..e60104e --- /dev/null +++ b/tests/figs/plot-grid/byrow-is-false.svg @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 + + + + + + + + + + + + + + + + + + + + +3 + + + + + + + + + + + + + + + + + + + + +2 + + diff --git a/tests/figs/plot-grid/byrow-is-true.svg b/tests/figs/plot-grid/byrow-is-true.svg new file mode 100644 index 0000000..d268743 --- /dev/null +++ b/tests/figs/plot-grid/byrow-is-true.svg @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 + + + + + + + + + + + + + + + + + + + + +2 + + + + + + + + + + + + + + + + + + + + +3 + + diff --git a/tests/testthat/test_plot_grid.R b/tests/testthat/test_plot_grid.R index 18d5546..7063e14 100644 --- a/tests/testthat/test_plot_grid.R +++ b/tests/testthat/test_plot_grid.R @@ -98,3 +98,18 @@ test_that("alignment", { plot_grid(p1, p2, ncol = 1, align = 'v', axis = "rl") + theme_map() ) }) + + + +test_that("labels reorder by byrow", { + + p_list <- lapply(1:3, \(x) ggplot()) + + expect_doppelganger("byrow is TRUE", + plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = TRUE) + ) + + expect_doppelganger("byrow is FALSE", + plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = FALSE) + ) +}) From affdb46db15a4e35d96c3330c9f06ba3bf12916c Mon Sep 17 00:00:00 2001 From: Doubt-0KB <98461207+Doubt-0KB@users.noreply.github.com> Date: Sat, 11 Jan 2025 16:23:22 +0900 Subject: [PATCH 3/3] change the tests --- tests/figs/plot-grid/byrow-is-false.svg | 85 ------------------------- tests/figs/plot-grid/byrow-is-true.svg | 85 ------------------------- tests/testthat/test_plot_grid.R | 15 ++--- 3 files changed, 7 insertions(+), 178 deletions(-) delete mode 100644 tests/figs/plot-grid/byrow-is-false.svg delete mode 100644 tests/figs/plot-grid/byrow-is-true.svg diff --git a/tests/figs/plot-grid/byrow-is-false.svg b/tests/figs/plot-grid/byrow-is-false.svg deleted file mode 100644 index e60104e..0000000 --- a/tests/figs/plot-grid/byrow-is-false.svg +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -1 - - - - - - - - - - - - - - - - - - - - -3 - - - - - - - - - - - - - - - - - - - - -2 - - diff --git a/tests/figs/plot-grid/byrow-is-true.svg b/tests/figs/plot-grid/byrow-is-true.svg deleted file mode 100644 index d268743..0000000 --- a/tests/figs/plot-grid/byrow-is-true.svg +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -1 - - - - - - - - - - - - - - - - - - - - -2 - - - - - - - - - - - - - - - - - - - - -3 - - diff --git a/tests/testthat/test_plot_grid.R b/tests/testthat/test_plot_grid.R index 7063e14..fd9f49d 100644 --- a/tests/testthat/test_plot_grid.R +++ b/tests/testthat/test_plot_grid.R @@ -102,14 +102,13 @@ test_that("alignment", { test_that("labels reorder by byrow", { - + # byrow=TRUE p_list <- lapply(1:3, \(x) ggplot()) + g <- plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = TRUE) + expect_equal(layer_data(g, 4)$label, 2) - expect_doppelganger("byrow is TRUE", - plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = TRUE) - ) - - expect_doppelganger("byrow is FALSE", - plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = FALSE) - ) + # byrow=FALSE + p_list <- lapply(1:3, \(x) ggplot()) + g <- plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = FALSE) + expect_equal(layer_data(g, 4)$label, 3) })