gg.matrix: Create Matrix of ggplot2 Plots

View source: R/gg.matrix.R

gg.matrixR Documentation

Create Matrix of ggplot2 Plots

Description

This function creates a matrix of ggplot2 plots.

Usage

gg.matrix(..., ncol = 1, nrow = 1, byrow = TRUE, strip.lab = NULL,
          strip.xlab = NULL, strip.ylab = NULL, strip.show = NULL,
          strip.labels = "topright", title = NULL, xlab = NULL, ylab = NULL,
          axis.labels = c("both", "x", "y", "none"), legend = 1, labeller = NULL,
          x.prop = NULL, y.prop = NULL, title.text.size = 10, strip.text.size = 10,
          axis.title.size = 10, axis.text.size = 10,
          filename = NULL, width = NA, height = NA, dpi = 600,
          check = TRUE, plot = TRUE)

Arguments

...

an expression indicating the names of the ggplot2 plot or a list of ggplot2 plots to be put into the matrix.

ncol

a numeric value indicating the number of columns. Note that at least one of the argument ncol or nrow needs to be specified.

nrow

a numeric value indicating the number of rows Note that at least one of the argument ncol or nrow needs to be specified.

byrow

logical: if TRUE (default), the plots are ordered by rows.

strip.lab

a character vector specifying the strip labels for the ggplot2 plots specified in the argument .... Note that the length of the character vector needs to match the number of plots specified in the argument ....

strip.xlab

a character vector specifying the strip labels for the x axis. Note that the length of the character vector needs to match the argument ncol.

strip.ylab

a character vector specifying the strip labels for the y axis. Note that the length of the character vector needs to match the argument nrow.

strip.show

logical: if TRUE, each plot's strip label will be shown. Note that the default setting NULL will show strip labels on the top and right plots only.

strip.labels

a character string specifying on which side the plots strip labels are printed, i.e., "topright" (default) for printing labels on the top and right part of the plot matrix, "bottomright" for printing labels on the bottom and right part of the plot matrix, "topleft" for printing labels on the top and left part of the plot matrix, or "bottomleft" for printing labels on the bottom and left part of the plot matrix.

title

a character string specifying the title of the plot.

xlab

a character string specifying the x label of the plot.

ylab

a character string specifying the y label of the plot.

axis.labels

a character string specifying which plot axis labels are printed, i.e., "both" (default) for printing axis labels on the x and y part of the plot matrix, "x" for printing the axis label on the x part of the plot matrix, "y" for printing axis label on y part of the plot matrix, or "none" for not printing any axis labels of the plot matrix.

legend

a numeric vector of length 2 specifying the location of the plot to use the legend for the plot matrix's legend, or a numeric value specifying the location of a plot according to the display order. By default, the plot displays the legend of the first plot.

labeller

labeller for facets.

x.prop

a numeric vector specifying how much area is given for each plot in the columns.

y.prop

a numeric vector specifying how much area is given for each plot in the rows.

title.text.size

a numeric value (default: 10) indicating the size argument in the element_text function for the plot.title argument, i.e. theme(plot.title = element_text(size = title.text.size))

strip.text.size

a numeric value (default: 10) indicating the size argument in the element_text function for the strip.text argument, i.e. theme(strip.text = element_text(size = strip.text.size))

axis.title.size

a numeric value (default: 10) indicating the size argument in the element_text function for the axis.title argument, i.e. theme(axis.title = element_text(size = axis.title.size))

axis.text.size

a numeric value (default: 10) indicating the size argument in the element_text function for the axis.text argument, i.e. theme(axis.text = element_text(size = axis.text.size))

filename

a character string indicating the filename argument including the file extension in the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the filename argument. Note that plots can only be saved when plot = TRUE.

width

a numeric value indicating the width argument (default is the size of the current graphics device) in the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) in the ggsave function.

dpi

a numeric value indicating the dpi argument (default: 600) in the ggsave function.

check

logical: if TRUE (default), argument specification is checked.

plot

logical: if TRUE (default), the matrix of ggplot2 plot is drawn.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

args

specification of function arguments

plot

ggplot2 object

Note

This function uses the function ggmatrix from the GGally package by Barret Schloerke, Di Cook, Joseph Larmarange, Francois Briatte, Moritz Marbach, Edwin Thoen, Amos Elberg, and Jason Crowley (2025).

Author(s)

Takuya Yanagida

References

Schloerke, B., Cook, D., Larmarange, J., Briatte, F., Marbach, M., Thoen, E., Elberg, A., & Crowley, J. (2025). GGally: Extension to 'ggplot2'. R package version 2.4.0. Retrieved from https://CRAN.R-project.org/package=GGally

Examples

## Not run: 
# Load ggplot2 package
library(ggplot2)

# Generate some random plots
p1 <- ggplot(mtcars, aes(mpg, hp, color = disp)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, drat, color = disp)) + geom_point()
p3 <- ggplot(mtcars, aes(mpg, wt, color = disp)) + geom_point()
p4 <- ggplot(mtcars, aes(mpg, qsec, color = disp)) + geom_point()

#————————————————————————————————————————————————————————————————————————————
# Matrix of ggplot2 Plots

# Example 1a: Arrange plots in four rows
gg.matrix(p1, p2, p3, p4, nrow = 4)

# Example 1b: Alternative specification using a list of plots
gg.matrix(list(p1, p2, p3, p4), nrow = 4)

# Example 1c: Arrange plots in two rows and four columns
gg.matrix(p1, p2, p3, p4, nrow = 2, ncol = 2)

# Example 1d: Alternative specification using a list of plots
gg.matrix(list(p1, p2, p3, p4), nrow = 2, ncol = 2)

#————————————————————————————————————————————————————————————————————————————
# 'strip.lab', 'strip.xlab', and 'strip.ylab' Arguments

# Example 2a: Strip labels for the y axis
gg.matrix(p1, p2, p3, p4, nrow = 4,
          strip.ylab = c("mpg - hp", "mpg - drat", "mpg - wt", "qmpg - sec"))

# Example 2b: Strip labels for the x axis
gg.matrix(p1, p2, p3, p4, ncol = 4,
          strip.xlab = c("mpg - hp", "mpg - drat", "mpg - wt", "qmpg - sec"))

# Example 2c: Strip label for each plot
gg.matrix(p1, p2, p3, p4, nrow = 2, ncol = 2, strip.show = TRUE,
          strip.lab = c("mpg - hp", "mpg - drat", "mpg - wt", "qmpg - sec"))

# Example 2d: Strip label for each plot and for the y axis
gg.matrix(p1, p2, p3, p4, nrow = 2, ncol = 2,
          strip.ylab = c("A", "B"), strip.show = TRUE,
          strip.lab = c("mpg - hp", "mpg - drat", "mpg - wt", "qmpg - sec"))

#————————————————————————————————————————————————————————————————————————————
# 'x.prop' and 'y.prop' Arguments

# Example 3: Modify area given for each plot in the columns and rows
gg.matrix(p1, p2, p3, p4, nrow = 2, ncol = 2,
          x.prop = c(0.4, 0.6), y.prop = c(0.80, 0.20))

#————————————————————————————————————————————————————————————————————————————
# 'strip.show' Argument

# Load data set "tips" in the GGally package
data("tips", package = "GGally")

# Create four plots depending on 'smoker' (No/Yes) and 'sex' (Female/Male)
plots <- tapply(tips, paste0(tips$smoker, tips$sex), function(y) {

  ggplot(y, aes(total_bill, tip)) + geom_point() + facet_grid(time ~ day)

})

# Example 4a: Show strip labels in the top row and the right most column
gg.matrix(plots, nrow = 2, ncol = 2, strip.ylab = c("Female", "Male"),
          strip.xlab = c("Non Smoker", "Smoker"))

# Example 4b: Show all strip labels
gg.matrix(plots, nrow = 2, ncol = 2, strip.ylab = c("Female", "Male"),
          strip.xlab = c("Non Smoker", "Smoker"), strip.show = TRUE)

# Example 4c: Do not show any strip labels
gg.matrix(plots, nrow = 2, ncol = 2, strip.ylab = c("Female", "Male"),
          strip.xlab = c("Non Smoker", "Smoker"), strip.show = FALSE)

#————————————————————————————————————————————————————————————————————————————
# Additional ggplot2 Arguments

# Create matrix of ggplot2 plots
p <- gg.matrix(p1, p2, p3, p4, nrow = 2, ncol = 2, plot = FALSE)

# Example 5: Additional ggplot2 arguments
p$plot +
 theme_bw() +
 theme(legend.position = "bottom", legend.box.margin = margin(-10, 0, 0, 0))

#————————————————————————————————————————————————————————————————————————————
# Save Plot

# Example 6: Save plot as PNG file
gg.matrix(p1, p2, p3, p4, nrow = 4, strip.ylab = c("hp", "drat", "wt", "qsec"),
          filename = "Plot_Matrix.png", width = 7, height = 10)

## End(Not run)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to gg.matrix in misty...