Skip to main content

Arrange several plots in one figure

Source: R/plot-arrange.R

Description

Put plots built with cgraph_plot() side by side, stacked, or in a grid. p1 | p2 places plots side by side and p1 / p2 stacks them; chains like p1 | p2 | p3 give equal columns, and the two nest: (p1 | p2) / p3 puts two plots above a third. cgraph_plot_grid() lays out any number of plots in rows and columns, e.g. a list of them.

Usage

cgraph_plot_grid(
  ...,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  widths = NULL,
  heights = NULL,
  spacing = NULL
)

## S3 method for class 'cgraph_plot'
Ops(e1, e2)

## S3 method for class 'cgraph_plots'
Ops(e1, e2)

Arguments

... Plots from cgraph_plot(), graphs from cgraph() (plotted with the defaults), or arrangements from cgraph_plot_grid(), | or /; or a single list of them.
nrow, ncol The number of rows and columns. With neither, the grid is as square as possible; with one, the other fits all the plots.
byrow Fill the grid row by row (default) rather than column by column.
widths, heights Relative column widths and row heights, e.g. widths = c(2, 1) makes the first column twice as wide. Default equal.
spacing Grid unit for the gap between plots. Default plot_options("spacing"), initially grid::unit(1, "lines").
e1, e2 Plots (or graphs, or arrangements) to combine.

Details

The arrangement is drawn when printed, like a single plot: print it with newpage = FALSE to draw it into the current grid viewport, or get its grob with cgraph_grob(). Style each plot before arranging it.

Value

A cgraph_plots object, drawn when printed.

See Also

plot_options() to change the default spacing.

Other plotting: cgraph_grob(), cgraph_layout(), cgraph_plot(), plot-styling, plot_default_options(), plot_options()

Examples

dag <- cgraph(A %-->% X + Y, X %-->% Y, class = "DAG")
admg <- cgraph(A %-->% X, X %-->% Y, X %<->% Y, class = "ADMG")
p1 <- cgraph_plot(dag, main = "DAG")
p2 <- cgraph_plot(admg, main = "ADMG") |> style_nodes(fill = "salmon")

p1 | p2

p1 / p2

(p1 | p2) / p1


dags <- lapply(1:6, function(i) generate_graph(5, p = 0.4, seed = i))
cgraph_plot_grid(lapply(dags, cgraph_plot), ncol = 3)