Arrange multiple (32) .png files in a grid

IdaFish picture IdaFish · Aug 18, 2014 · Viewed 7k times · Source

I've been pulling my hair out for the past week trying to figure out elementary R coding but can't seem to get anywhere (haven't used R since 2013 not that its a great excuse).

All I want is a 4x8 grid made up of 32 .png files (maps I've made), and I want to do it without loading one image file at a time (http://www.statmethods.net/advgraphs/layout.html).

So I think I can load the images within the folder writing (please correct me if my beliefs are bs)

img <- list.files(path='c:/a',patt='compo[0-32].*',full.names=T)

Then I was thinking maybe in the lines of par(mfrow=c()), layout, grid.arrange (writing png plots into a pdf file in R), grid.raster (How to join efficiently multiple rgl plots into one single plot?) - which I've read up on and experimented with accordingly not resulting in anything worthwhile..

The latter I employed only with the following outcome enter image description here

It made me giggle. I don't really think lattice is the way to go anyway.

Any help would be greatly appreciated!

Answer

Richard DiSalvo picture Richard DiSalvo · Aug 15, 2018

Another approach is to read the PNG images with readPNG then use grid and gridExtra:

library(png)
library(grid)
library(gridExtra)

plot1 <- readPNG('plot1.png')
plot2 <- readPNG('plot2.png')

grid.arrange(rasterGrob(plot1),rasterGrob(plot2),ncol=1)