R Markdown:如何在网格中排列文件中的许多图像

问题描述 投票:0回答:1

我有 100 张图像保存为 .png 文件。我想将它们全部显示为 10x10 的图像网格,这样每个图像都有自己的标题,并且图像网格共享一个标题。我想使用 R Markdown 来做到这一点。这可能吗?

r r-markdown
1个回答
0
投票

使用 {ggplot2} 和 {cowplot} 的方法:

library(ggplot2)
library(cowplot)

## data frame with image positions on a 10 x 10 grid,
## increasing from top left to bottom right (change as needed):
positions <- expand.grid(x = 0:9, y = 9:0)

## paths to images, use `list.files()` etc. for convenience:
file_names <- c('path/to/img1.png', 'path/to/img2.png')

## tile images with `draw_image` on an empty `ggplot` object:
seq_along(file_names) |>
  Map(f = \(i){
    draw_image(file_names[i], 
               x = .1 * positions$x[i],
               y = .1 * positions$y[i],
               width = .1, height = .1
               )
  }) |>
  Reduce(f = \(collage, next_tile) collage + next_tile, init = ggplot()) +
  coord_fixed() +
  theme_void()
© www.soinside.com 2019 - 2024. All rights reserved.