View source: R/plot_multiple.R
plot_multiple | R Documentation |
The function takes a list of plots made using ggplot() as input and arranges them in a grid.
plot_multiple( ..., plots = NULL, cols = 1, by_row = FALSE, layout_matrix = NULL, shared_legend = FALSE, position = c("bottom", "right") )
... |
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". |
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)/>
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.