[GIS] A raster map without a legend

legendrraster

I simply download a map from here http://neo.sci.gsfc.nasa.gov/view.php?datasetId=MCD12C1_T1enter image description here. this map is a tiff file.

  mapd <- raster("C:\\MCD12C1_T1_2011-01-01_rgb_1440x720.TIFF")
  plot(mapd)

you can see there is no legend at all, any help

Best Answer

The colors are stored with the TIFF file, but either the legend class names are not in there or else they are not read by raster. Therefore you will have to manually copy the legend names from the Web site and manually create an informative legend, but you can automatically display the codes for the colors.

The first color in the color table is the background color and the remaining nonzero colors are the class colors. You can automatically determine the number of classes in several ways. Because having class counts can be useful, the following code computes them and as a result finds out how many classes there are (16 plus background).

library(raster)
mapd <- raster("...") # Use the path to your raster data source

classes <- table(getValues(mapd)) # Tabulation of class counts
colors <- mapd@legend@colortable  # Class colors
plot(mapd, col=colors[1:length(classes)+1], colNA=colors[1], axes=FALSE, box=FALSE)

Figure

Related Question