png
包不是必需的,下面是另一个利用
EBImage
. 除非有意变换图像,否则EBImage将保持纵横比。
此解决方案需要更多的工作来放置打印标签
text()
但增加的成本可能会被更大的灵活性所抵消。
# Starting from EBImage
library(EBImage)
fn <- system.file("images", "sample-color.png", package="EBImage")
# Single EBImage object with same four pictures
pngs <- readImage(rep(fn, 4))
# Select spacing and margin for EBImage display/plot function
spacing <- 50 # pixels between images
margin <- 50 # pixels around edge
nx <- 2 # number of rows for composite
ny <- 2 # number of columns for composite
dm <- dim(pngs)
nframes <- dm[length(dm)]
yadj <- -0.2 # offset for labels in y direction
# Indices, coordinates, and labels for text
index <- seq_len(nframes)
ix <- (index - 1)%%nx
iy <- (index - 1)%/%nx
xx <- ix*(dm[1] + spacing) + dm[1]/2
yy <- iy*(dm[2] + spacing)
labs <- sprintf("%s. Image %d", letters[index], index)
# Plot composite and add "titles" with text() function
plot(pngs, all = TRUE, nx = nx, margin = margin, spacing = spacing)
text(xx, yy, labs, adj = c(0.5, yadj))
# To eliminate ANY space in the composite make the following
# changes and re-run the code from "# Indices, coordinates..."
spacing <- 0
margin <- 0
yadj <- 1.2
dev.new(width = nx*dm[1]/200, height = ny*dm[2]/200) # 200 dpi image