multiplot | R Documentation |
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.
multiplot(..., plotlist = NULL, file, cols = 1, layout = NULL)
... |
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. |
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).
No return value. The function displays the plots or saves them to a file if 'file' is specified.
Edi Prifti (IRD)
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.