[R-sig-Geo] cropping in raster package loses color table?

MacQueen, Don macqueen1 at llnl.gov
Tue Mar 1 01:12:18 CET 2011


The colortable is apparently not preserved when using crop() from the
raster package. This is something I would find useful.

I'm only just beginning to learn how to use the raster package, so perhaps
there's another way to subset a raster image that preserves the
colortable? Or I'm making some mistake?

(also, is there a better way to set the colortable?)

There is a reproducible example below.

Thanks
-Don


## based on the example in ?crop
r <- raster(nrow=45, ncol=90)
r[] <- sample(1:10,ncell(r),replace=TRUE)
r at legend@colortable <-c("#476BA1","#D1DEFA","#DECACA","#D99482","#EE0000",
      "#AB0000","#B3AEA3","#68AB63","#1C6330","#B5CA8F")

e <- extent(-160, 10, 30, 60)
rc <- crop(r, e)

cat('original colortable\n')
str(r at legend)
cat('\ncropped colortable\n')
str(rc at legend)

With output:

original colortable
Formal class 'RasterLegend' [package "raster"] with 4 slots
  ..@ type      : chr(0)
  ..@ values    : NULL
  ..@ color     : NULL
  ..@ colortable: chr [1:10] "#476BA1" "#D1DEFA" "#DECACA" "#D99482" ...

cropped colortable
Formal class 'RasterLegend' [package "raster"] with 4 slots
  ..@ type      : chr(0)
  ..@ values    : NULL
  ..@ color     : NULL
  ..@ colortable: NULL




> sessionInfo()
R version 2.12.2 (2011-02-25)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] C

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base

other attached packages:
[1] raster_1.7-46   rgdal_0.6-33    maptools_0.7-38 lattice_0.19-17
sp_0.9-77       foreign_0.8-42

loaded via a namespace (and not attached):
[1] grid_2.12.2


-- 
Don MacQueen

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



More information about the R-sig-Geo mailing list