Learn R Programming

plotrix (version 3.0-2)

color.scale: Turn values into colors.

Description

Transform numeric values into colors

Usage

color.scale(x,redrange=c(0,1),greenrange=c(0,1),bluerange=c(0,1),
  extremes=NA,na.color=NA,xrange=NULL)

Arguments

x
a numeric vector, matrix or data frame
redrange,greenrange,bluerange
color ranges into which to scale x
extremes
The colors for the extreme values of x.
na.color
The color to use for NA values of x.
xrange
An explicit range to use in the transformation.

Value

  • A vector or matrix of hexadecimal color values.

Details

color.scale calculates a sequence of colors by a linear transformation of the numeric values supplied into the ranges for red, green and blue. If only one number is supplied for a color range, that color remains constant for all values of x. If more than two values are supplied, the x values will be split into equal ranges (one less than the number of colors) and the transformation carried out on each range. Values for a color range must be between 0 and 1. If extremes is not NA, the ranges will be calculated from its values using col2rgb, even if ranges are also supplied. extremes allows the user to just pass the extreme color values in any format that col2rgb will accept.

If the user wants to specify a range of values with xrange, it must at least include the range of x values. This can be useful when there is a notional range like 0-100 cover, or when several series of values with different ranges are to be assigned the same color scale.

The user may not want the color scheme to be continuous across some critical point, often zero. In this case, color scale can be called separately for the values below and above zero. See the second example for color2D.matplot.

See Also

rescale, col2rgb, smoothColors

Examples

Run this code
# go from green through yellow to red with no blue
 x<-rnorm(20)
 y<-rnorm(20)
 # use y for the color scale
 plot(x,y,col=color.scale(y,c(0,1,1),c(1,1,0),0),main="Color scale plot",
  pch=16,cex=2)

Run the code above in your browser using DataLab