I have downloaded a raster with land cover data from http://www.diva-gis.org/gdata.
library(tmap)
library(raster)
grid_cov <- raster('VEN_cov/VEN_cov.grd')
grid_cov
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:
unique(values(grid_cov))
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.
Attempt 2: Pass colors in the same order as per values(grid_cov)
cover_palette <- c('#F0F8FF',
'#CDC673',
'#A4D3EE',
'#00CD00',
'#FAEBD7',
'#FF7256',
'#FFEFDB',
'#008B00',
'#1874CD',
'#CD2626',
'#7CCD7C',
'#FF7F50',
'#EEE8CD',
'#98FB98')
tm_shape(grid_alt) + tm_raster(legend.show = F,palette=cover_palette,style='order' )
save_tmap(filename="~/Desktop/attempt2.png",dpi=75)
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',
'#A4D3EE','#00CD00',
'#FAEBD7','#FF7256',
'#FFEFDB','#008B00',
'#1874CD','#CD2626',
'#7CCD7C','#FF7F50',
'#EEE8CD','#98FB98')
colors <- data.frame(id = lc_id,color=cover_palette)