If you are willing to use lattice
graphics, the levelplot
method
defined in the rasterVis
package is able to
display categorical data with a legend based on the RAT:
library(raster)
library(rasterVis)
r <- raster(ncol=10, nrow=10)
values(r) <- rep(1:4, each=25)
r <- ratify(r)
rat <- levels(r)[[1]]
rat$legend <- c('Class A', 'Class B', 'Class C', 'Class D')
levels(r) <- rat
levelplot(r)
Besides, I have
commited some changes
in the
development version available at GitHub
to manage a Raster*
whose RAT levels are not all present in the
data:
rcrop <- crop(r,extent(r,6,10,1,10))
levelplot(rcrop)
rcrop <- crop(r,extent(r,1,5,1,10))
levelplot(rcrop)