[R-sig-Geo] writeRaster and RGB vs Gray

MacQueen, Don macqueen1 at llnl.gov
Fri Apr 26 21:25:46 CEST 2013

Here's a little example script using functions from the raster package,

r1 <- r2 <- r3 <- raster(ncol=25,nrow=25,
     xmn=0, ymn=0, xmx=1, ymx=1)
r1[] <- round(runif(25^2,0,255))
r2[] <- round(runif(25^2,0,255))
r3[] <- round(runif(25^2,0,255))
s <- stack(r1,r2,r3)


writeRaster(s,'s.tif', format='GTiff', overwrite=TRUE,

When I open the file s.tif in QGIS, it's fine; I see the same image as in

But the Mac platform's default viewer, Preview.app, thinks the file's
color model is "Gray", rather than "RGB", and it does not render properly.
Preview does recognize, for example, a jpeg photo as having an RGB color
(Adobe Reader (for Mac) wouldn't open it at all.)

Is there some better set of arguments to give writeRaster that will
somehow make the file more broadly recognizable as RGB?

Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

More information about the R-sig-Geo mailing list