7

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

2
  • In addition to the absolutely right solution provided by @cengel, have a look at this short tutorial on how to place the legend outside a plot. I think that would make sense in your particular case...
    – fdetsch
    Commented Oct 4, 2013 at 11:36
  • Cross-posted: stackoverflow.com/questions/19136330/…
    – user32309
    Commented Oct 30, 2015 at 11:40

1 Answer 1

4

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

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.