lab2col {basetheme} | R Documentation |
Labels to Colors
Description
Assigns colors to the provided vector of labels.
Usage
lab2col(x, pal, ref = x, NAcol)
Arguments
x |
vector of labels (always transformed to character) |
pal |
colors used to build the palette (defaults to colors set by theme) |
ref |
reference for assigning colors (defaults to elements of |
NAcol |
color to be used for NA values (defaults to color set by theme) |
Details
This function assigns colors to each unique level of label vector x
.
Mainly used for consistently assigning colors to a unique set of labels,
especially when the order of original labels might change (see examples).
Color of NA values and values outside of specified reference can be set using
NAcol
argument. Set this to NA to omit the display of such values.
ref
parameter can be specified to select the order of color assignment:
first element from ref
will be assigned the first color from pal
,
second element - second color, and so on.
The list of provided colors is expanded by first adding shades and then adding tints. However if the number of groups exceeds the number of provided colors by more than 3 times the colors will be repeated.
When x
is not specified - a function that generates colors based on
pal
and ref
is returned.
Value
a vector of colors for each element in x
or, when x
is missing, a function.
Author(s)
Karolis Koncevičius
See Also
num2col
Examples
# iris example
pairs(iris[,1:4], col=lab2col(iris$Species))
# iris example with one group missing from reference
pairs(iris[,1:4], col=lab2col(iris$Species, ref=c("setosa", "versicolor")))
# example of using a coloring function
# "color" function below will consistently assign colors to values in "ref".
color <- lab2col(ref=unique(chickwts$feed))
par(mfrow=c(1,2))
means <- tapply(chickwts$weight, chickwts$feed, mean)
barplot(means, col=color(names(means)), las=2)
means <- sample(means)
barplot(means, col=color(names(means)), las=2)