Skip to content

Commit 34819eb

Browse files
authored
Merge pull request #203 from Doubt-0KB/master
fixed `byrow=FALSE` reorder not only plots but also labels
2 parents e1334a2 + affdb46 commit 34819eb

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

R/plot_grid.R

+5-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,11 @@ plot_grid <- function(..., plotlist = NULL, align = c("none", "h", "v", "hv"),
177177
if (is.null(cols)) cols <- ceiling(num_plots/rows)
178178
if (is.null(rows)) rows <- ceiling(num_plots/cols)
179179

180-
# if the user wants to layout the plots by column, we use the calculated rows to reorder plots
181-
if (!isTRUE(byrow)) plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))]
180+
# if the user wants to layout the plots by column, we use the calculated rows to reorder plots and labels
181+
if (!isTRUE(byrow)){
182+
plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))]
183+
labels <- labels[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))]
184+
}
182185

183186
# Align the plots (if specified)
184187
grobs <- align_plots(plotlist = plots, align = align, axis = axis, greedy = greedy)

tests/testthat/test_plot_grid.R

+14
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ test_that("alignment", {
9898
plot_grid(p1, p2, ncol = 1, align = 'v', axis = "rl") + theme_map()
9999
)
100100
})
101+
102+
103+
104+
test_that("labels reorder by byrow", {
105+
# byrow=TRUE
106+
p_list <- lapply(1:3, \(x) ggplot())
107+
g <- plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = TRUE)
108+
expect_equal(layer_data(g, 4)$label, 2)
109+
110+
# byrow=FALSE
111+
p_list <- lapply(1:3, \(x) ggplot())
112+
g <- plot_grid(plotlist = p_list, ncol = 2, labels = 1:3, byrow = FALSE)
113+
expect_equal(layer_data(g, 4)$label, 3)
114+
})

0 commit comments

Comments
 (0)