r - How to map numbers to colours? -
i trying view file(unsigned character, pixel =1440 , lines=720) map. tried piece of code given bellow. first, downloaded may problem code using continuous colour scheme, though have discrete data (which classification scheme). how can map numbers colours ? please example of wanted scale shown below:
conne <- file("c:\\landcover.bin", "rb") dfr<- readbin(conne, integer(), size=1, n=720*1440, signed=f) y<-matrix((data=dfr), ncol=1440, nrow=720) image(y)
the raster
package provides method use categorical data. read page of ratify
details.
first let's create rasterlayer
data:
library(raster) dfr <- readbin('biome1440s.bin', integer(), size=1, n=720*1440, signed=f) r <- raster(nrow=720, ncol=1440) r[] <- dfr
now define rasterlayer
factor ratify
. should change levels using information instead of letters
:
r <- ratify(r) rat <- levels(r)[[1]] rat$soil <- letters[1:15] levels(r) <- rat
and finally, categorical rasterlayer
can displayed levelplot
method of rastervis
package.
library(rastervis) mypal <- c('lightblue', terrain.colors(14)) ## using par.settings levelplot(r, par.settings=rastertheme(region=mypal)) ## or col.regions levelplot(r, col.regions=mypal)
edited: levelplot
uses lattice
graphics, while plot
uses base
graphics. cannot used (unless use gridbase
package). however, can overlay additional information using +.trellis
, layer
functions latticeextra
package. since wrld_simpl
spatialpolygonsdataframe
can use sp.polygons
function sp
package plot it.
library(maptools) ## needed wrld_simpl data(wrld_simpl) ## spatialpolygonsdataframe levelplot(r, col.regions=mypal) + layer(sp.polygons(wrld_simpl, lwd=0.5))