Given a vector of class labels, return a color vector corresponding to the class values. There is an option to set a different color for NA. A vector with corresponding color values and a key specifying color value of each class label is returned.

get.colvector(labels,col, NA.flag=FALSE, NA.col="grey")

Arguments

labels

vector, labels for which color vector is desired. Can be numeric, character or factor. If it is a factor colors are coded in the order of levels of a factor

col

vector, a character vector containing colors for each unique class label. Also accepts colors from colorRampPallette etc

NA.flag

logical, a logical flag where NA in labels vector are replaced with NA.col. Default NA.flag is FALSE.

NA.col

character, color value specifying how NA should be color coded. Default color value is grey. Will only color code NA when NA.flag is TRUE and when present

Details

if the vector labels has names, the returned color coded vector will also have names.

Value

labels.list

A list, with following elements -

labels.col : character, a color coded character vector of labels key : a matrix, a 2 x unique class labels character matrix showing how values in labels have mapped to col

Examples

set.seed(123) x<-sample(letters[1:4], 20, replace=TRUE) table(x)
#> x #> a b c d #> 3 7 8 2
x.col<-get.colvector(x,c("red","blue","green","orange")) x.col
#> $labels.col #> [1] "green" "green" "green" "blue" "green" "blue" "blue" "blue" #> [9] "green" "red" "orange" "blue" "blue" "red" "blue" "green" #> [17] "orange" "red" "green" "green" #> #> $key #> [,1] [,2] [,3] [,4] #> "a" "b" "c" "d" #> col "red" "blue" "green" "orange" #>
#lets add some NAs x[c(1,4,5)] = NA x.col<-get.colvector(x,c("red","blue","green","orange"), NA.flag=TRUE, NA.col="grey") x.col
#> $labels.col #> [1] "grey" "green" "green" "grey" "grey" "blue" "blue" "blue" #> [9] "green" "red" "orange" "blue" "blue" "red" "blue" "green" #> [17] "orange" "red" "green" "green" #> #> $key #> [,1] [,2] [,3] [,4] [,5] #> "a" "b" "c" "d" NA #> col "red" "blue" "green" "orange" "grey" #>