How to merge images into one file in a defined order
Note that the solution outlined by Thomas introduces some whitespace into the multipane image not present in the source image. Adding the arguments xaxs = 'i'
and yaxs='i'
into plot()
will remove it.
library("png") # for reading in PNGs
# example image
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
# setup plot
dev.off()
par(mai=rep(0,4)) # no margins
# layout the plots into a matrix w/ 12 columns, by row
layout(matrix(1:120, ncol=12, byrow=TRUE))
# do the plotting
for(i in 1:120) {
plot(NA,xlim=0:1,ylim=0:1,bty="n",axes=0,xaxs = 'i',yaxs='i')
rasterImage(img,0,0,1,1)
}
# write to PDF
dev.print(pdf, "output.pdf")
You can plot them all together using the rasterImage
function and the png package. Here's a simple showing how to read in a PNG and then plot it (a bunch of times).
library("png") # for reading in PNGs
# example image
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
# setup plot
par(mar=rep(0,4)) # no margins
# layout the plots into a matrix w/ 12 columns, by row
layout(matrix(1:120, ncol=12, byrow=TRUE))
# do the plotting
for(i in 1:120) {
plot(NA,xlim=0:1,ylim=0:1,xaxt="n",yaxt="n",bty="n")
rasterImage(img,0,0,1,1)
}
# write to PDF
dev.print(pdf, "output.pdf")
You would need to modify this slightly so that it calls each image object, rather than just plotting img
over and over again.
Result: