[R-sig-Geo] color schemes for landcover plots of multiple rasters

Tom Philippi tephilippi at gmail.com
Tue Sep 25 00:06:17 CEST 2012


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:
> I'm plotting a series ( a few hundred )  of  rasters that are filled with
> landcover data,   integers
>
> Normally, these are  integers such as  c(11,12,21,22,23,31,32,41,42,43,51,
>  etc etc)
> where each value corresponds to a landcover type.
>
> I want all of these plots to have consistent colors and legends. However,
> not every plot has
> a complete set of land cover types.   What I tried was the following.
>
> I took all the landcover values in the raster and reclassified them so that
> I got a  seq from
> 1..n.   and then I made a vector of colors the same length
>
> However, when a land type if missing I still get   errors.  I tried using
>  breaks() and that
> helped a little but I would still get  errors
>
> Here is a toy example
>
> r <- raster(nrow=10,ncol=10)
> #  slot in some values
> r[1:12]<-0
> r[13:34]<-1
> r[35:76]<-2
> r[77:95]<-3
> r[96:100]<-4
>
> #  define colors
> myColors <- c("black","green","orange","pink","blue")
>
> plot(r,col=myColors)
>
> #  swicth the 4 to 0
> r <- subs(r,data.frame(Id= c(0,1,2,3,4), v=c(0,1,2,3,0)))
>
> plot(r,col=myColors)
>
> Another way to ask this is how do I get the function to recognize that I
> watch categorical colors instead of
> continuous ones. or should I change the colors dynamically according to the
> actual values found in that
> particular map.
>
> I tried  breaks() no joy
>
>
> Steve
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list