library(data.table)
library(raster)
Ras <- raster(extent(0,15,0,15), res=1)
fullRas <- randomPolygons(Ras, numTypes=2)
names(fullRas) <- "mapcodeAll"
uniqueComms <- unique(fullRas)
reducedDT <- data.table(mapcodeAll=uniqueComms,
communities=sample(1:1000,length(uniqueComms)),
biomass=rnbinom(length(uniqueComms),mu=4000,0.4))
biomass <- rasterizeReduced(reducedDT, fullRas, "biomass")
# The default key is the layer name of the fullRas, so even
# if the reducedDT is miskeyed
setkey(reducedDT, biomass)
communities <- rasterizeReduced(reducedDT, fullRas, "communities")
setColors(communities) <- c("blue", "orange", "red")
if (interactive()) Plot(biomass, communities, fullRas, new = TRUE)
Run the code above in your browser using DataLab