How to legend a raster using directly the raster attribute table and displaying the legend only for class displayed in the raster?

StackOverflow https://stackoverflow.com/questions/19586945

Domanda

I would like to use the raster attribute table information to create the legend of a raster such as the raster 1 and display the legend only for the class displayed in the raster. I build an example to explain what I would like to get.

1/ Build the raster

r <- raster(ncol=10, nrow=10)
values(r) <-sample(1:3,ncell(r),replace=T)

2/ Add the Raster Attribute Table

r <- ratify(r) # build the Raster Attibute table
rat <- levels(r)[[1]]#get the values of the unique cell frot the attribute table
rat$legend <- c('Class A', 'Class B', 'Class C')
levels(r) <- rat

3/ Plot the raster Fig 1

my_col=c('blue','red','green')
plot(r,col=my_col,legend=F,box=F,axes=F)
legend(x='top', legend =rat$legend,fill = my_col)

I would like to replace the legend =rat$legend parammeter by a properties of the raster linked to the ratser attribute table. I have tried different combination using levels() such as c(levels(r)[[1]][1]) but I generate a list and not a character not usable in the legend parameter.

4/ Crop and plot the raster to a part with only 2 classes (here the 4 pixels at the down right extent) Fig 2

rcrop<-crop(r,extent(r,9,10,9,10))
plot(rcrop,col=my_col,legend=F,box=F,axes=F)

For this second fig, I thus would like to diplay automatically only the legend of the class displayed on the raster 2.

Raster with legend

Cropped raster with legend


Here is the solution proposed by Roman 4. Solution proposed by Roman

È stato utile?

Soluzione

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)

rat.png

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)

rat crop

rcrop <- crop(r,extent(r,1,5,1,10))
levelplot(rcrop)

rat crop2

Altri suggerimenti

Maybe query the rcrop raster to find out which are the levels in the cropped raster and pass that to the legend?

legend(x = 'top', legend = unique(getValues(rcrop)), fill = my_col)

Thanks. Based on what you proposed I found a solution:

plot(rcrop,col=my_col[unique(getValues(rcrop))],legend=F,box=F,axes=F)
legend_full<-data.frame(levels(rcrop))
legend(x='top', legend =as.character(unlist(legend_full[unique(getValues(rcrop)),2])),fill = my_col[unique(getValues(rcrop))])

It works and allows to be independent of the values displayed in the raster.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top