[R-sig-Geo] plotting raster - color values per pixel

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri May 11 10:43:44 CEST 2012


On Fri, May 11, 2012 at 9:20 AM, Mathieu Rajerison
<mathieu.rajerison at gmail.com> wrote:

>> In col, I must specify a palette, but how can I use my color vector?
>>
>> Here is the line of code for the moment (which doesn't work):
>> cols <- hsv(ifelse(values(r) > .5, 0, .7), values(r), 1)
>> plot(r, col=topo.colors(20), axes=FALSE)

 You can do this by instead of plotting the 'r' raster (which is your
values) you plot a raster containing the values 1 to nrows*ncols. and
pass col as a vector of colours of length nrows*ncols. Then each cell
in the raster maps exactly to its colour value.

 Disadvantage: the legend is now 1:(nrows*ncols)

 The other way is to create a 3-band raster from the red, green, blue
components of the colours.

Barry



More information about the R-sig-Geo mailing list