Description Usage Arguments Details Examples
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
1 | namedColVec(col, fac, by = "level", na.col = "gray", na.name = "NA")
|
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 |
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
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')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.