Does anyone know how to import a legend for a raster layer (loaded as a .tif) into R such that it can be attached to the raster file itself and used in plotting?

In this case, I am using the GLC2009 V2.3 data, available from: http://due.esrin.esa.int/page_globcover.php

The legend is stored separately as a .xls file, as well as .lyr and .dsr files. I believe the latter 2 extensions are for ArcINFO files but regardless I have never used either in R.

I can import the .xls legend as follows easily:

    GLC_Legend <- read.xlsx("./Globcover2009_V2.3/Globcover2009_Legend.xls", 
            sheetIndex=1, header=TRUE)

    #   Value                                                     Label Red Green Blue
    # 1    11         Post-flooding or irrigated croplands (or aquatic) 170   240  240
    # 2    14                                         Rainfed croplands 255   255  100

But I am lost as to how I could utilize this data frame to re-create the color map, as well as to how I could use the .lyr or .dsr files in its stead. Any know-how on importing colormaps from files for mapping purposes in R would be wonderful!


You can make a paletted raster by assigning a colortable in the legend.

If you have a raster called r and a data frame like yours above called ctab, with value and red/green/blue colour values, you can do something like this:

> ctable = rep(NA,max(ctab$value)+1)
> ctable[ctab$value+1] = 
> r@legend@colortable = ctable
> plot(r)

Note the +1s are needed because the colortable starts at zero (I think).

|improve this answer|||||
  • You're the bomb! Had to rearrange your code just slightly but the rgb() function was just what I needed. Worked like a charm. Didn't have a ton of familiarity with grDevices package but I'll read up on it now, for sure. Thanks for the input! – Henry Hawkins Wells Jun 17 '15 at 0:25
  • 1
    The colortable stuff in package:raster is poorly documented, there's not even any real functions for it which is why you end up setting the colours explicitly with @-stuff. Maybe I'll write some helper functions and see if Rob H wants to put them in the package... – Spacedman Jun 17 '15 at 7:06

I ran into this same issue. Then I checked the download page again:

A coloured version of the map in GeoTIFF format:

For those of us who don't want to go through the trouble of recreating the colored map with R.

|improve this answer|||||

As an extra, you can easily convert the "Globcover2009_Legend.xls" into a CSV file, then import the CSV easily into R:

# XLS --> CSV
ogr2ogr -f CSV Globcover2009_Legend.csv Globcover2009_Legend.xls
> globcover_legend <- read.csv("Globcover2009_Legend.csv")
> str(globcover_legend)
'data.frame':   23 obs. of  5 variables:
$ Value: int  11 14 20 30 40 50 60 70 90 100 ...
$ Label: Factor w/ 23 levels "Artificial surfaces and associated areas (Urban areas >50%)",..: 20 21 12 15 6 3 17 5 18 11 ...
$ Red  : int  170 255 220 205 0 0 170 0 40 120 ...
$ Green: int  240 255 240 205 100 160 200 60 100 130 ...
$ Blue : int  240 100 100 102 0 0 0 0 0 0 ...
|improve this answer|||||

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.