| gg.matrix | R Documentation |
This function creates a matrix of ggplot2 plots.
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)
... |
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 |
nrow |
a numeric value indicating the number of rows Note
that at least one of the argument |
byrow |
logical: if |
strip.lab |
a character vector specifying the strip labels for the
ggplot2 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 |
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 |
strip.show |
logical: if |
strip.labels |
a character string specifying on which side the plots
strip labels are printed, i.e., |
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., |
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: |
strip.text.size |
a numeric value (default: |
axis.title.size |
a numeric value (default: |
axis.text.size |
a numeric value (default: |
filename |
a character string indicating the |
width |
a numeric value indicating the |
height |
a numeric value indicating the |
dpi |
a numeric value indicating the |
check |
logical: if |
plot |
logical: if |
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 |
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).
Takuya Yanagida
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
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.