lab2col {basetheme}R Documentation

Labels to Colors


Assigns colors to the provided vector of labels.


lab2col(x, pal, ref = x, NAcol)



vector of labels (always transformed to character)


colors used to build the palette (defaults to colors set by theme)


reference for assigning colors (defaults to elements of x)


color to be used for NA values (defaults to color set by theme)


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.


a vector of colors for each element in x or, when x is missing, a function.


Karolis Koncevičius

See Also



 # 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))
 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)

[Package basetheme version 0.1.3 Index]