I have downloaded a raster with land cover data from http://www.diva-gis.org/gdata.


grid_cov <- raster('VEN_cov/VEN_cov.grd')

class       : RasterLayer 
dimensions  : 1416, 1656, 2344896  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -73.5, -59.7, 0.5, 12.3  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 
data source : /Users/altons/Dropbox/datacamp/geospatial course/VEN_cov/VEN_cov.grd 
names       : VEN_cov 
values      : 1, 22  (min, max)

Values in raster are:

20 15 14  2 13 16 12  1 19 22  7 17 18  8

which according to this document(pages 19 & 20)represent the type of land cover.

Now I have tried to map specific colors to land cover codes but my attempts have failed so far:

Attempt 1: pass color in ascending order as per land cover codes

cover_palette <- c("#008B00", "#00CD00", "#7CCD7C", "#98FB98", "#FFEFDB", "#FAEBD7", "#A4D3EE", "#CDC673", "#FF7256", "#FF7F50", "#EEE8CD", "#1874CD", "#F0F8FF", "#CD2626")

tm_shape(grid_alt) + tm_raster(legend.show = F,palette=cover_palette,style='cat' )

Bits in red/blue are mountains... which is wrong based on my definition of colors.

enter image description here

Attempt 2: Pass colors in the same order as per values(grid_cov)

cover_palette <- c('#F0F8FF',

tm_shape(grid_alt) + tm_raster(legend.show = F,palette=cover_palette,style='order' )

enter image description here

The last plot - the red bits are mountains which I thought I have colored in white....

Anyway - I think my question is: If I created a data.frame with all the colors then how can I merge it with the RasterLayer object? and therefore use colors$color instead of guessing!

my data frame looks like this:

lc_id = c(20,15,14,2,13,16,12,1,19,22,7,17,18,8)
cover_palette <- c('#F0F8FF','#CDC673',

colors <- data.frame(id = lc_id,color=cover_palette)

1 Answer 1


Install the rasterVis package, for we are going to use its levelplot functionality...



Read the raster in:

gc = raster("./VEN_cov.grd")

Convert to categorical:

gc = ratify(gc)

Now I don't like numeric categorical levels, because its easy to mess them up with indices, so I'll always make character ones. If you have short names for the categories (like "Urban", "Forest", "Woodland") use them here. Long names will mess up the key. Anyway:

levels(gc)[[1]]$code = paste0("GLC-",levels(gc)[[1]]$ID)

Your colors data frame is good, but use character colours instead of factors so they don't get converted to numbers inadvertently, and sort it so its in the same order as levels(gc):

colors <- data.frame(id = lc_id,color=cover_palette,stringsAsFactors=FALSE)
csort = colors[order(colors$id),]

Now the magic:


enter image description here

which I think is right. There's a few bits of red urban, and a lot of forest classes.

To do in tmap, use:

> tm_shape(gc)+tm_raster(palette=csort$color)
  • Thanks! Will give it a try when I get home - I was using tmap to overlay various pieces of information and apply some cosmetics, is is possible to convert the levelplot output back to a raster? Not sure if that make sense
    – Altons
    Jul 18, 2017 at 9:12
  • Seems to be workable with tmap! See edit.
    – Spacedman
    Jul 18, 2017 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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