multiplot: Create Multiple Plots on One Page

View source: R/global.visu.R

multiplotR Documentation

Create Multiple Plots on One Page

Description

This function arranges multiple plots on a single page using grid layout. It allows for custom arrangement of the plots and can save the plots to a file if needed. It supports both direct plotting and generating a multi-plot layout from a list of plots.

Usage

multiplot(..., plotlist = NULL, file, cols = 1, layout = NULL)

Arguments

...

One or more ggplot objects to be displayed.

plotlist

A list of ggplot objects to be displayed. This is an alternative to passing the plots as '...'.

file

Optional; a character string specifying the file path to save the plot as a file (e.g., PNG, PDF). Default is 'NULL', which means the plot is shown in the R graphics window.

cols

The number of columns to arrange the plots in. Default is '1'. This is used to calculate the layout if 'layout' is not provided.

layout

A matrix specifying the layout of the plots on the page. If 'NULL', the layout is automatically calculated based on the number of plots and the 'cols' parameter.

Details

The function arranges multiple ggplot objects in a grid layout, with the number of columns determined by the 'cols' argument. The function will automatically adjust the number of rows to fit all the plots. If 'layout' is provided, it will override the 'cols' argument to control the layout.

If 'file' is provided, the function will save the multi-plot layout to the specified file. The supported formats depend on the device used (e.g., PNG, PDF).

Value

No return value. The function displays the plots or saves them to a file if 'file' is specified.

Author(s)

Edi Prifti (IRD)

Examples

library(ggplot2)
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
p3 <- ggplot(mtcars, aes(disp, hp)) + geom_point()

# Display the plots in a 2x2 grid
multiplot(p1, p2, p3, cols=2)

# Save the plots to a file
multiplot(p1, p2, p3, file="my_plots.pdf", cols=2)


predomics/predomicspkg documentation built on Dec. 11, 2024, 11:06 a.m.