SpaDES (version 1.1.4)

setColors<-: Set colours for plotting Raster* objects.

Description

setColors works as a replacement method or a normal function call.

Usage

setColors(object, ..., n) <- value

## S3 method for class 'RasterLayer,numeric,character': setColors(object, ..., n) <- value

## S3 method for class 'RasterLayer,missing,character': setColors(object, ..., n) <- value

## S3 method for class 'RasterStack,numeric,list': setColors(object, ..., n) <- value

## S3 method for class 'Raster,missing,list': setColors(object, ..., n) <- value

setColors(object, value, n)

## S3 method for class 'RasterLayer,character,numeric': setColors(object, value, n)

## S3 method for class 'RasterLayer,character,missing': setColors(object, value)

Arguments

object
A Raster* object.
...
Additional arguments to colorRampPalette.
n
An optional vector of values specifiying the number of levels from which to interpolate the color palette.
value
Named list of hex color codes (e.g., from RColorBrewer::brewer.pal), corresponding to the names of RasterLayers in x.

Value

  • Returns a Raster with the colortable slot set to values.

See Also

brewer.pal, colorRampPalette.

Examples

Run this code
library(raster)
  library(igraph) # need pipe for one example below

  on.exit({
    detach("package:raster")
    detach("package:igraph")
  })
  ras <- raster(matrix(c(0,0,1,2), ncol=2, nrow=2))

  # Use replacement method
  setColors(ras, n=3) <- c("red", "blue", "green")
  Plot(ras, new=TRUE)

  # Use function method
  ras <- setColors(ras, n=3, c("red", "blue", "yellow"))
  Plot(ras, new=TRUE)

  # Using the wrong number of colors, e.g., here 2 provided,
  # for a raster with 3 values... causes interpolation, which may be surprising
  ras <- setColors(ras, c("red", "blue"))
  Plot(ras, new=TRUE)

  # Real number rasters - interpolation is used
  ras <- raster(matrix(runif(9), ncol=3, nrow=3)) %>%
    setColors(c("red", "yellow")) # interpolates when real numbers, gives warning
  Plot(ras, new=TRUE)

  # Factor rasters, can be contiguous (numerically) or not, in this case not:
  ras <- raster(matrix(sample(c(1,3,6), size=9, replace=TRUE), ncol=3, nrow=3))
  levels(ras) <- data.frame(ID=c(1,3,6), Names=c("red", "purple", "yellow"))
  ras <- setColors(ras, n=3, c("red", "purple", "yellow"))
  Plot(ras, new=TRUE)

  # if a factor rastere, and not enough labels are provided, then a warning
  #   will be given, and colors will be interpolated
  #   The level called purple is not purple, but interpolated betwen red and yellow
  ras <- setColors(ras, c("red", "yellow"))
  Plot(ras, new=TRUE)

Run the code above in your browser using DataCamp Workspace