assignColors {plot.matrix} | R Documentation |
assignColors
Description
Assign to each value in x
a color according to the choices of breaks
and col
.
Usage
assignColors(x, breaks = NULL, col = heat.colors, na.col = "white")
Arguments
x |
numeric or non-numeric vector |
breaks |
vector with breaks |
col |
vector with colors or color function |
na.col |
color for NA or out-of-range values |
Details
Depending if x
is a numeric or non-numeric vector colors
are assigned to each value.
In case of a numeric vector breaks
can be
a number, giving the number of intervals covering the range of
x
,a vector of two numbers, given the range to cover with 10 intervals, or
a vector with more than two numbers, specify the interval borders
In case of a non-numeric vector breaks
must contain all values which are
will get a color.
If breaks
is not given then a sensible default is choosen:
in case of a numeric vector derived from pretty
and
otherwise all unique values/levels are used.
col
can be either be a vector of colors or a function which generates
via col(n)
a set of n
colors. The default is to use
heat.colors
.
Possible color functions in R packages can be found by vignette('plot.matrix')
.
Value
vector of color with the same length as x
with the attributes
breaks
the breaks used, col
the color coding and na.col
the color for NA
and out-of-range entries
Examples
## numeric vector
x <- runif(10)
assignColors(x)
# set breaks
assignColors(x, breaks=15)
assignColors(x, breaks=c(0,1))
# set colors
assignColors(x, col=c("red", "green", "blue"))
assignColors(x, col=topo.colors)
# NA and out-of-range
x[5] <- NA
assignColors(x, breaks=seq(0.5, 1, by=0.1), na.col="red")
## logical vector
l <- sample(c(NA, TRUE, FALSE), size=10, replace=TRUE)
assignColors(l)
assignColors(l, breaks=c("FALSE", "TRUE"), col=c("red", "blue"))
## character vector
t <- sample(letters, size=10, replace=TRUE)
assignColors(t)
assignColors(t, col=rainbow(5))