plot_multiple: Arrange multiple plots in a grid

View source: R/plot_multiple.R

plot_multipleR Documentation

Arrange multiple plots in a grid

Description

The function takes a list of plots made using ggplot() as input and arranges them in a grid.

Usage

plot_multiple(
  ...,
  plots = NULL,
  cols = 1,
  by_row = FALSE,
  layout_matrix = NULL,
  shared_legend = FALSE,
  position = c("bottom", "right")
)

Arguments

...

plot objects

plots

List of plot objects. Defaults to NULL.

cols

An integer specifying the number of columns. The number of rows is calculated automatically. Defaults to 1.

by_row

If TRUE, the generated layout_matrix is filled by row. Default is FALSE.

layout_matrix

A layout matrix. Defaults to NULL.

shared_legend

If TRUE the plots share the same legend. Defaults to FALSE.

position

Position of the shared legend. Can be either "bottom" or "right".

References

The function is based on the multiplot function in "Cookbook for R". Please see the original code at <http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/>

Examples

library(ggplot2)
library(tibble)

df <- tibble(x = seq_len(10), y = runif(10), z = runif(10) * 1.5,
 a = sample(c(0, 1), 10, TRUE))
plot_1 <- ggplot(data = df) + geom_point(aes(x = x, y = y))
plot_2 <- ggplot(data = df) + geom_point(aes(x = x, y = z))

plot_multiple(plot_1, plot_2)
plot_multiple(plot_1, plot_2, cols = 2)
plot_multiple(plots = list(plot_1, plot_2))

plot_3 <- ggplot(data = df) + geom_point(aes(x = x, y = y, colour = a))
plot_4 <- ggplot(data = df) + geom_point(aes(x = x, y = z, colour = a))
plot_multiple(plot_3, plot_4, shared_legend = TRUE, position = "bottom")
plot_multiple(plot_3, plot_4, shared_legend = TRUE, position = "right")


edsandorf/gizmo documentation built on Oct. 7, 2022, 3:45 p.m.