[R] How can I map numbers to colours with raster?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Oct 29 11:55:59 CET 2012

On Mon, Oct 29, 2012 at 9:13 AM, Jonsson <amen.alyaari at bordeaux.inra.fr> wrote:
> This code will read binary file and display it as a map. may problem is that
> this code is using a continuous colour scheme, even though I have discrete
> data (which  is a classification scheme). How can I map numbers to colours
> with raster? Please
> require(raster)
> conne <- file("C:\\lai.bin", "rb")
> sd<- readBin(conne, integer(), size=1,  n=360*720, signed=F)
>  y<-t(matrix((data=sd), ncol=360, nrow=720))
>  r = raster(y)
> extent(r) = extent(c(xmn=-180,xmx=180,ymn=-90,ymx=90))
> plot(r)

 The raster package can handle categorical rasters, but it seems
poorly documented.

 If I read in a categorical raster (land cover classification from
CORINE) then I get a raster that plots using the colour table defined
in the GeoTIFF. This comes from a 'legend' attribute which the raster
object has. This is an undocumented object of class ".RasterLegend".

 I think Robert "Raster" Hijmans has been meaning to document all this
at some point, I've been meaning to dig into it too. Its probably
worth asking further questions on R-sig-geo.


More information about the R-help mailing list