Skip to contents

Combines several images to a grid

Usage

image_combine(
  ...,
  labels = NULL,
  nrow = NULL,
  ncol = NULL,
  col = "white",
  col_bg = "forestgreen",
  badge = TRUE,
  border = NA,
  cex = 0.75,
  font = 2,
  verbose = TRUE,
  mar = c(0.2, 0.2, 0.2, 0.2),
  oma = c(0, 0, 0, 0),
  row_labels = NULL,
  col_labels = NULL,
  strip_col = NULL,
  strip_bg = NULL,
  strip_cex = 0.95,
  strip_size = NULL,
  tag_levels = NULL,
  tag_prefix = "",
  tag_suffix = "",
  design = NULL,
  widths = NULL,
  heights = NULL,
  panel_scale = NULL,
  file = NULL,
  width = NULL,
  height = NULL,
  units = c("px", "in", "cm", "mm"),
  dpi = 300
)

Arguments

...

a comma-separated name of image objects or a list containing image objects. Objects with a plot() method, such as the results of image_detect_dl() and image_segment_dl(), can also be given. To change how one of them is drawn, write the plot() call directly, e.g. image_combine(img, plot(det, cex = 0.8, lwd = 0.5)): the call is run inside its own panel with all the arguments given, instead of being drawn beforehand.

labels

A character vector with the same length of the number of objects in ... to indicate the plot labels.

nrow, ncol

The number of rows or columns in the plot grid. Defaults to NULL, i.e., a square grid is produced.

col

The color for the plot labels. Defaults to "white" when badge = TRUE and "black" otherwise.

col_bg

The background color for the label badge. Defaults to "forestgreen".

badge

Logical. Whether to draw a background badge for the labels. Defaults to TRUE.

border

The border color for the badge. Defaults to NA (no border).

cex

The character expansion factor for the labels. Defaults to 0.75.

font

An integer which specifies which font to use for text. Defaults to 2 (bold).

verbose

Shows the name of objects declared in ... or a numeric sequence if a list with no names is provided. Set to FALSE to supress the text.

mar, oma

Margins for each panel and outer margins for the combined plot. Defaults to mar = c(0.2, 0.2, 0.2, 0.2) and oma = c(0, 0, 0, 0). Set mar = 0 or mar = c(0, 0, 0, 0) to eliminate margins and maximize the plotting area.

row_labels, col_labels

Optional character vectors of labels to display as full-height left strips (row_labels, length nrow) and full-width top strips (col_labels, length ncol). Useful for publication comparison grids.

strip_col, strip_bg

Text color and background fill color for row_labels and col_labels strips. When NULL (default), strip_col inherits col and strip_bg inherits col_bg.

strip_cex

Character expansion factor for strip labels. Defaults to 0.95.

strip_size

Relative width/height of the row and column label strips compared to an image panel (1). Defaults to NULL, which automatically computes strip dimensions from the current graphics device aspect ratio so panels align edge-to-edge without gaps. Can also be set explicitly as a scalar or length-2 vector c(row_width, col_height).

tag_levels

Automatic panel tags, as in patchwork::plot_annotation(): "a" (a, b, c, ...), "A" (A, B, C, ...), "1" (1, 2, 3, ...), "i" (i, ii, iii, ...) or "I" (I, II, III, ...). A character vector with one tag per image can also be given. Ignored when labels is given. Defaults to NULL (no automatic tags).

tag_prefix, tag_suffix

Text added before and after each automatic tag, e.g. tag_prefix = "(" and tag_suffix = ")" give (a), (b), ... Defaults to "".

design

A free layout, as in patchwork::plot_layout(design = ). Either a string with one line per row and one letter per cell, where repeating a letter makes a panel span several cells and # leaves a cell empty (e.g. "AB\nCD\nEE": four panels in two rows and a fifth spanning both columns), or an integer matrix as in graphics::layout() (0 = empty). Panels fill the areas in alphabetical (or numeric) order. Overrides nrow and ncol; row_labels and col_labels are ignored. Defaults to NULL.

widths, heights

Relative widths of the columns and heights of the rows of the grid (or of design), recycled as needed, e.g. heights = c(1, 1, 0.4) for a short bottom row. Defaults to NULL (equal sizes).

panel_scale

Size of the page on which panels that are not images (plots) are drawn, relative to their cell. Values above 1 shrink text, margins and lines of those panels (e.g. 2 halves them). Defaults to NULL, which uses a page of at least 3 inches on each side, so short cells still fit the axes.

file

Optional file name to save the grid instead of drawing it on the current device. The format comes from the extension: png, jpg/jpeg, tif/tiff, bmp, pdf or svg (images are embedded as rasters in pdf and svg). Defaults to NULL (draw on the current device).

width, height

Size of the saved file, in units. When only one is given, the other follows the aspect of the grid; when both are NULL, the size is computed from the images (each cell as wide as the widest image, rows at the median image aspect, scaled down to at most 10000 px). Used only with file.

units

Units of width and height: "px" (default), "in", "cm" or "mm".

dpi

Resolution of the saved file, in dots per inch. Defaults to 300.

Value

A grid with the images in ... (invisibly, the list of panels), or, when file is given, the file name invisibly.

Details

Besides images, any object with a plot() method (e.g. the result of video_detect_dl()) or a ggplot can be a panel, given directly or as a plot(x, ...) call. Panels that are not images are drawn off-screen at the size of their cell and the resolution of the current device (at least 150 ppi) and then placed in the grid, so plot methods that change par() or the layout do not break the grid. A list of images can be combined with other panels, e.g. image_combine(imgs, plot(res, type = "abundance"), design = "AB\nCD\nEE").

Images are no longer downsampled below the resolution of the current device, so large frames keep their detail when written to a high-resolution png().

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

if (FALSE) { # interactive()
library(pliman)
img1 <- image_pliman("sev_leaf.jpg")
img2 <- image_pliman("sev_leaf_nb.jpg")
image_combine(img1, img2)

# Remove margins to optimize plotting area:
image_combine(img1, img2, mar = 0)

# Combine an image with a deep learning result drawn with custom arguments
farm <- image_pliman("farm.jpg")
det <- image_detect_dl(farm, model = "yolo26n", plot = FALSE)
image_combine(farm, plot(det, cex = 0.8, lwd = 0.5), mar = 0, tag_levels = "a", ncol = 1)

# Automatic tags as in patchwork: (A), (B)
image_combine(img1, img2, tag_levels = "A", tag_prefix = "(", tag_suffix = ")")

# Free layout, as in patchwork: two images on top, a third spanning both columns
image_combine(img1, img2, img1, design = "AB\nCC", heights = c(1, 0.5),
              tag_levels = "a")

# Save to a file 17 cm wide at 600 dpi (the height follows the grid)
image_combine(img1, img2, file = file.path(tempdir(), "grid.png"),
              width = 17, units = "cm", dpi = 600)
}