Learn R Programming

patchwork (version 0.0.1)

wrap_plots: Wrap plots into an assemble

Description

While the use of + is a natural way to add plots together, it can be difficult to string together multiple plots programmatically if the number of plots is not known beforehand. wrap_plots makes it easy to take a list of plots and add them into one composition, along with layout specifications.

Usage

wrap_plots(..., ncol = NULL, nrow = NULL, byrow = NULL,
  widths = NULL, heights = NULL, guides = NULL, tag_level = NULL)

Arguments

...

multiple ggplots or a list containing ggplot objects

ncol

The dimensions of the grid to create - if both are NULL it will use the same logic as facet_wrap() to set the dimensions

nrow

The dimensions of the grid to create - if both are NULL it will use the same logic as facet_wrap() to set the dimensions

byrow

Analogous to byrow in matrix(). If FALSE the plots will be filled in in column-major order

widths

The relative widths and heights of each column and row in the grid. Will get repeated to match the dimensions of the grid.

heights

The relative widths and heights of each column and row in the grid. Will get repeated to match the dimensions of the grid.

guides

A string specifying how guides should be treated in the layout

tag_level

A string ('keep' or 'new') to indicate whether auto-tagging should behave. See plot_annotation().

Value

A ggassemble object

Examples

Run this code
# NOT RUN {
library(ggplot2)

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
p4 <- ggplot(mtcars) + geom_bar(aes(carb))
p5 <- ggplot(mtcars) + geom_violin(aes(cyl, mpg, group = cyl))

# Either add the plots as single arguments
wrap_plots(p1, p2, p3, p4, p5)

# Or add them as a list...
plots <- list(p1, p2, p3, p4, p5)
wrap_plots(plots)

# }

Run the code above in your browser using DataLab