-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorjam-rainbowjammulti.R
76 lines (72 loc) · 2.12 KB
/
colorjam-rainbowjammulti.R
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#' Rainbow categorical colors across multiple groups
#'
#' Rainbow categorical colors across multiple groups
#'
#' This function applies `colorjam::rainbowJam()` to multiple
#' vectors, such that the categorical colors are not duplicated,
#' and are assigned as well-spaced across the rainbow for
#' each group as possible.
#'
#' @family colorjam assignment
#'
#' @return `list` of categorical colors, length equal to the
#' input `length(ns)`, with color vectors with lengths `ns`.
#'
#' @param ns `integer` vector of `n` values passed to
#' `rainbowJam()`.
#' @param do_plot `logical` indicating whether to plot the result
#' using `jamba::showColors()`.
#' @param ... additional arguments are passed to `rainbowJam()`.
#'
#' @examples
#' opar <- par("mfrow"=c(3, 1));
#' on.exit(par(opar));
#' ns <- c(A=8, B=3, C=4);
#' colorlist1 <- rainbowJamMulti(ns,
#' do_plot=TRUE,
#' main="rainbowJamMulti()");
#'
#' # basic assignment in order
#' colorset2 <- colorjam::rainbowJam(sum(ns));
#' colorlist2 <- split(colorset2, rep(seq_along(ns), ns))
#' jamba::showColors(colorlist2, main="sequential assignment")
#'
#' # re-assign the same colors
#' colorlist3 <- lapply(ns, colorjam::rainbowJam);
#' jamba::showColors(colorlist3, main="independent color assignment");
#' par(opar);
#'
#' @export
rainbowJamMulti <- function
(ns,
do_plot=FALSE,
...)
{
# generate a sorting offset
noff <- (seq_along(ns) - 1) / 100;
# create a sort order across each vector of ns
nseqs <- lapply(seq_along(ns), function(n){
nseq <- head(
seq(from=0 + noff[n], to=360 + noff[n], length.out=ns[n] + 1),
ns[n]);
names(nseq) <- paste0("set", n, "_num", seq_len(ns[n]));
nseq;
});
nseqs
# obtain full set of rainbow categorical colors
ncolors <- colorjam::rainbowJam(
n=length(unlist(nseqs)),
...);
names(ncolors) <- names(sort(unlist(nseqs)));
nseqcolors <- lapply(nseqs, function(i){
unname(ncolors[names(i)])
});
if (length(names(ns)) > 0) {
names(nseqcolors) <- names(ns);
}
if (do_plot) {
jamba::showColors(nseqcolors,
...);
}
return(nseqcolors);
}