一种选择是使用magick::image_montage。唯一的技巧是创建一个 2x2 网格并插入一个空白图像作为第三个对象。
library(magick)
#> Linking to ImageMagick 6.9.12.3
#> Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fontconfig, x11
imgs_url <- c('https://i.stack.imgur.com/TqnwC.png',
'https://i.stack.imgur.com/qIDXh.png',
'https://i.stack.imgur.com/B36ko.png')
imgs <- image_read(imgs_url)
imgs <- c(imgs[1:2], image_blank(width = 3, height = 5), imgs[3])
image_montage(imgs, tile = '2x2', geometry = "x200+3+5")
创建于 2021-07-13 由 reprex package (v2.0.0)
如果要按名称对图像进行分组,一种方法是在函数中捕获上述工作流程,并添加 grepl 语句来标识要绘制的图像子集。
我没有所有图片,因此未经测试。但它看起来像这样。
my_files <- c("bart Cum Oil.jpg",
"bart GOR.jpg",
"bart Legend.jpg",
"bart Oil Rate.jpg",
"lisa Cum Oil.jpg",
"lisa GOR.jpg",
"lisa Legend.jpg",
"lisa Oil Rate.jpg",
"marge Cum Oil.jpg",
"marge GOR.jpg",
"marge Legend.jpg",
"marge Oil Rate.jpg")
# remove oil rate image paths
my_files <- my_files[!grepl('Oil Rate', my_files)]
# create function
make_grid <- function(group, path_to_files){
f <- files[grepl(group, files)]
imgs <- image_read(f)
imgs <- c(imgs[1:2], image_blank(width = 3, height = 5), imgs[3])
image_montage(imgs, tile = '2x2', geometry = "x200+3+5")
}
# create lisa image grid
make_grid('lisa', my_files)
# create all image grids
lapply(c('bart', 'lisa', 'marge'), make_grid, path_to_files = my_files)