Thursday, 15 March 2018

cartography - Legend of a raster map with categorical data


I would like to plot a raster containing 4 different values (1) with a categorical text legend describing the categories such as 2 but with colour boxes.


I've tried using legend such as :



legend( 1,-20,legend = c("land","ocean/lake", "rivers","water bodies"))

but I don't know how to associate one value to the displayed color. Is there a way to retrieve the colour displayed with 'plot' and to use it in the legend?


Initial rasterenter image description here


Raster with legend enter image description here



Answer



You are not providing a reproducible code, so I made up a simple example that hopefully will allow you to tailor it to your needs.


library(raster)
r <- raster(volcano)
plot(r, legend = FALSE, col = rev(terrain.colors(4)))

legend("topright", legend = c("category 1", "category 2", "category 3", "category 4"), fill = rev(terrain.colors(4)))

enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...