diff --git a/R/plot_grid.R b/R/plot_grid.R index 2501762..064ad00 100644 --- a/R/plot_grid.R +++ b/R/plot_grid.R @@ -177,8 +177,11 @@ 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) 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) + ) +})