2

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

2
  • 1
    What exactly is your question? There is a legend under the map in the link you provided.
    – Lou
    Apr 2, 2015 at 14:57
  • I want to show the legend my self using R. I know it is available in that website but does not appear when I plot for my publications using R.
    – usersam
    Apr 2, 2015 at 15:03

2 Answers 2

4

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

3

You have to provide a color palette:

library("raster")
mapd <- raster("MCD12C1_T1_2011-01-01_rgb_1440x720.TIFF")

plot(mapd, col=terrain.colors(255), axes=FALSE)

plot

The rasterVis package has lattice based methods for plotting Raster objects (spplot(...)):

library("rasterVis")
spplot(mapd)

rasterVis::spplot

1
  • Thanks. is there a way to keep the same colours as in the map shown in my queston and just add the legend?
    – usersam
    Apr 2, 2015 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.