Here is R script and my question is How can I plot 12 images per plot or save 12 images per image file because I have total 864 images.

RAD1998 <- raster(paste(getwd(), "/1998bil /1998ASC5min_ppt_spas1214_0001_19980202_0810_UTC.asc.bil", sep = ""))

list.ras <- mixedsort(list.files(paste(getwd(), "/1998bil/", sep = ""), full.names = T, pattern = ".asc.bil")) 
RAD1998.all <- stack(list.ras)
  • I would use levelplot from rasterVis package to plot RAD1998.all. – user32309 Aug 8 '14 at 4:49
  • levelpot is a good suggestion, oscarperpinan.github.io/rastervis. I am studying now. Thanks, Nahm – Nahm Aug 8 '14 at 5:19
  • level plot is great but I have one problem if levelplot index is in static then it works but not in a loop. Do you have any suggestions? Here is a codes. Thanks, png(paste("1998",outlist[[1]],".png",sep="")) levelplot(RAD1998.all[[1:4]]) dev.off() png(paste("1998",outlist[[5]],".png",sep="")) levelplot(RAD1998.all[[5:8]]) dev.off() png(paste("1998",outlist[[9]],".png",sep="")) levelplot(RAD1998.all[[9:12]]) dev.off() for(i in seq(from=1, to=length(list.ras), by=4)){ png(paste("1998_",outlist[[i]],".png",sep="")) levelplot(RAD1998.all[[i:i+3]]) dev.off() } – Nahm Aug 8 '14 at 7:49
  • You need to stack your rasters first, then use levelplot, i.e levelplot(RAD1998.all). – user32309 Aug 8 '14 at 7:56
  • I already stacked it. RAD1998.all <- stack(list.ras) – Nahm Aug 8 '14 at 8:00

If you choose rasterVis::levelplot, the layout argument will help you. Details about this argument can be found in the help page of lattice::xyplot:

‘layout’ is a numeric vector of length 2 or 3 giving the number of columns, rows, and pages (optional) in a multipanel display.


r <- raster()
ll <- list()

for (i in 1:48) ll[[i]] <- init(r, runif)
s <- stack(ll)

trellis.device('png', file = 'myPic%02d.png')
levelplot(s, layout = c(3, 4))
  • It worked very well. I don't need a for loop. Very neat. Thanks,Nahm – Nahm Aug 24 '14 at 6:53
  • I like this method. It works with even California map. – Nahm Aug 24 '14 at 7:22

You can control the number of plots per graphic device using the mfrow and mfcol arguments in par().

  for(i in 1:12) {plot(runif(100),runif(100)*0.05)}
  • I can see3 by 4 plots per plot page but somehow all of images are are showing linear dots. I don't know what to do – Nahm Aug 7 '14 at 22:28
  • Not sure what you mean. My example is just an x,y plot. You do realize that png is not a multipage device? If you want plot numerious rasters I would recommend a pdf device. After setting a graphic device and par() settings, you will also need to create a loop to plot each raster. Something like: for(i in 1:nlayer(RAD1998.all)) {plot(RAD1998.all[[i]])} – Jeffrey Evans Aug 7 '14 at 22:39
  • I need time to figure out PDF. But I tried it with Tiff file and now I can save 864 GTiff files and I need to combine second suggestion and 1st suggestion. Good progress. Thanks a lot. – Nahm Aug 7 '14 at 23:47
  • I am getting there. Now I can see 3 by 4 images correctly in every single plot and I still generate 864 images files. – Nahm Aug 8 '14 at 4:25
  • After I combined your suggestions and couple of other sources, then it works now and thanks a lot. Here is codes. for(i in seq(from=1, to=length(list.ras), by=4)){ pdf(paste("myOut",i,".pdf",sep="")) par(mfrow=c(2,2)) for(j in 1:4){ plot(RAD1998.all[[i+j]]) } dev.off() } dev.off() – Nahm Aug 8 '14 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.