Skip to content
Prev 16125 / 29559 Next

color schemes for landcover plots of multiple rasters

Steven--

I'm sure that there are several ways to do this, but I convert the
thematic raster to be a factor, then use the levels of that factor to
subset from my full color palette and plot the factor raster.

In my case I have a seamless geoTiff of NLCD 2006 Level1 data (NPS
makes NLCD, NALC, and CCAP data available as seamless geoTiffs, see
http://science.nature.nps.gov/im/monitor/npscape/index.cfm).  The tifs
come with a RAT (Raster Attribute Table) in the corresponding
*.tif.vat.dbf file, which gives class numbers, names, and Red Green
Blue values for our universal color scheme.  I read that into a
dataframe named NLCDclassesL1, and generate a Color value via rgb:

NLCDclassesL1 <-
foreign::read.dbf("E:/NPScape_2012/Seamless/GeoTIFFs/Landcover_Level1_NLCD2006.tif.vat.dbf",as.is=TRUE)
]
NLCDclassesL1$Color <- with(NLCDclassesL1,rgb(Red,Green,Blue))


If you don't have a RAT with colors defined, you can create your own
dataframe with ID (numeric, matching the values in your grid) and
Color with records for all possible classes, with ID==0 Color=NA for
the missing value class outside your polygon.

AOAc is the result of cropping the large raster to the extent of my
AOA polygon, then masking via rasterize().  It is still numeric, so I
create AOAf as thematic, generate the RAT via levels(), and glue on
the appropriate thematic colors and labels using match:

AOAf <- as.factor(AOAc)
rat <- levels(AOAf)[[1]]  # also has statistics of pixel counts
rat$landcover <-  NLCDclassL1$CLASSNAME[match(rat$ID,NLCDclassL1$Value)]
rat$Color <- NLCDclassL1$Color[match(rat$ID,NLCDclassL1$Value)]

plot(AOAf,col=rat$Color)

Note that you can associate your RAT with the raster file via levels()
<- or possibly ratify(), but the documentation is sketchy, so I don't
know how to get the right colors from an associated RAT file.

I'm still playing with trellis.par.set() to map the colors in spplot().

I hope that this helps; email for full example code.

Tom 2
On Sun, Sep 23, 2012 at 10:27 PM, steven mosher <moshersteven at gmail.com> wrote: