namedColVec: Generate a named color vector

Description Usage Arguments Details Examples

View source: R/aheatmap.R

Description

ggplot2 use named color vector for scale_colour_manual. This is an efficient way to generate colors. the namedColVec function facilitates this by requring a color vec and a factor. The number of levels in factor should equal the number of colors. Note this is different from the buildcolorfacs function

Usage

1
namedColVec(col, fac, by = "level", na.col = "gray", na.name = "NA")

Arguments

col

color vector

fac

the factor as a reference

by

colors to be named in the order of levels in the factor or the alphabetical order (in this case, fac is not required to be a factor)

na.col

color for NA. it is named by na.name

na.name

name for NA, na.col

return

a named color vector. color for NA is always at the end

Details

when NA is present, we deliberately tell the function what color and what name the user want it to be. To extract just the unique values without NA, we combine unique+na.omit or levels+na.omit

Examples

1
2
3
4
5
6
7
8
9
vv <- letters[c(5, 1, 3, 2, 26)]
vvNA <- c(vv, NA)
vvfac <- factor(vv)
vvNAfac <- factor(vvNA)
cols <- c('black', 'red', 'green', 'blue', 'yellow')
namedColVec(cols[1:length(unique(na.omit(vv)))], vv, by='alphabetical')
namedColVec(cols[1:length(unique(na.omit(vvNA)))], vvNA, by='alphabetical')
namedColVec(cols[1:length(unique(na.omit(vvfac)))], vvfac, by='level')
namedColVec(cols[1:length(unique(na.omit(vvNAfac)))], vvNAfac, by='level')

nickytong/GenAnalysis documentation built on July 20, 2019, 8:57 a.m.