get.colvector: Returns a color vector of unique values of interest.

Description Usage Arguments Details Value Author(s) Examples

View source: R/utilar.R

Description

Given a vector of class labels, return a color vector correponding to the unique 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.

Usage

1
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

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

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 : matrix, a character matrix showing how values in labels have mapped to col

Author(s)

Arshi Arora

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  set.seed(123)
  x<-sample(letters[1:4], 20, replace=TRUE)
  #table(x)
  #x
  #a b c d
  #4 5 4 7

  x.col<-get.colvector(x,c("red","blue","green","orange"))
  #x.col$labels.col
  #[1] "blue"   "orange" "blue"   "orange" "orange" "red"    "green"  "orange"
  #[9] "green"  "blue"   "orange" "blue"   "green"  "green"  "red"    "orange"
  #[17] "red"    "red"    "blue"   "orange"

  #x.col$key
  #[,1]  [,2]   [,3]    [,4]
  #ul  "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"   "orange" "blue"   "grey"   "grey"   "red"    "green"  "orange"
  #[9] "green"  "blue"   "orange" "blue"   "green"  "green"  "red"    "orange"
  #[17] "red"    "red"    "blue"   "orange"
  #x.col$key
  #[,1]  [,2]   [,3]    [,4]     [,5]
  #ul  "a"   "b"    "c"     "d"      "NA"
  #col "red" "blue" "green" "orange" "grey"

arorarshi/utilar documentation built on July 6, 2020, 8:29 a.m.