Skip to content

Commit

Permalink
fix code and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Doubt-0KB committed Oct 22, 2024
1 parent 1b133f4 commit 2d266f6
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/plot_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
85 changes: 85 additions & 0 deletions tests/figs/plot-grid/byrow-is-false.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions tests/figs/plot-grid/byrow-is-true.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/testthat/test_plot_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
})

0 comments on commit 2d266f6

Please sign in to comment.