Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 161: by column with plot_grid #163

Merged
merged 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
clipping is now configurable.
- Plot assemblies generated by the patchwork package are now supported
in `as_grob()`, `ggdraw()`, and `plot_grid()`.

## Minor changes

- `plot_grid()` now accepts `byrow` as an argument to place plots by row or by column onto the plot area. (@BrianLang, #162).

# cowplot 1.0.0

Expand Down
18 changes: 15 additions & 3 deletions R/plot_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#' @param scale Individual number or vector of numbers greater than 0. Enables you to scale the size of all or
#' select plots. Usually it's preferable to set margins instead of using `scale`, but `scale` can
#' sometimes be more powerful.
#' @param byrow Logical value indicating if the plots should be arrange by row (default) or by column.
#' @param rows Deprecated. Use \code{nrow}.
#' @param cols Deprecated. Use \code{ncol}.
#' @examples
Expand Down Expand Up @@ -96,6 +97,13 @@
#' align = "v"
#' )
#'
#' # can arrange plots on the grid by column as well as by row.
#' plot_grid(
#' p1, NULL, p2, NULL, p3, NULL,
#' ncol = 2,
#' byrow = TRUE
#' )
#'
#' # can align top of plotting area as well as bottom
#' plot_grid(
#' p1, p5,
Expand Down Expand Up @@ -131,7 +139,7 @@ plot_grid <- function(..., plotlist = NULL, align = c("none", "h", "v", "hv"),
label_fontfamily = NULL, label_fontface = "bold", label_colour = NULL,
label_x = 0, label_y = 1,
hjust = -0.5, vjust = 1.5, scale = 1., greedy = TRUE,
cols = NULL, rows = NULL ) {
byrow = TRUE, cols = NULL, rows = NULL) {

# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
Expand All @@ -158,8 +166,6 @@ plot_grid <- function(..., plotlist = NULL, align = c("none", "h", "v", "hv"),
rows <- nrow
}

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

# calculate grid dimensions
if (is.null(cols) && is.null(rows)){
Expand All @@ -171,6 +177,12 @@ 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 (!byrow) plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = byrow)))]

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

if ("AUTO" %in% labels)
labels <- LETTERS[1:num_plots]
else if ("auto" %in% labels)
Expand Down
10 changes: 10 additions & 0 deletions man/plot_grid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions tests/figs/plot-grid/colwise-arranging.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/testthat/test_plot_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ test_that("basic plot arranging works", {
expect_doppelganger("scaling plots",
plot_grid(p1, p2, p3, labels = "AUTO", scale = c(.9, .7, .5), nrow = 1) + theme_map() # add theme_map() for plot title
)

expect_doppelganger("colwise arranging",
plot_grid(p1, NULL, p2, p3, NULL, byrow = FALSE) + theme_map() # add theme_map() for plot title
)
})


Expand Down