Debian Codegraph @

R/color_branches.R

raw
1 # Copyright (C) Tal Galili
2 #
3 # This file is part of dendextend.
4 #
5 # dendextend is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # dendextend is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # A copy of the GNU General Public License is available at
16 # http://www.r-project.org/Licenses/
17 #
18
19
20
21
22
23
24
25
26 #' @title Check if all the elements in a vector are unique
27 #' @description
28 #' Checks if all the elements in a vector are unique
29 #' @export
30 #' @param x a vector
31 #' @param ... ignored.
32 #' @return
33 #' logical (are all the elements in the vector unique)
34 #'
35 #' @source
36 #'
37 #' \url{https://www.mail-archive.com/[email protected]/msg77592.html}
38 #' OLD (no longer working): https://r.789695.n4.nabble.com/Is-there-a-function-to-test-if-all-the-elements-in-a-vector-are-unique-td931833.html
39 #'
40 #' @seealso \link{unique}
41 #'
42 #' @examples
43 #'
44 #' all_unique(c(1:5, 1, 1))
45 #' all_unique(c(1, 1, 2))
46 #' all_unique(c(1, 1, 2, 3, 3, 3, 3))
47 #' all_unique(c(1, 3, 2))
48 #' all_unique(c(1:10))
49 all_unique <- function(x, ...) {
50 anyDuplicated(x) == 0L
51 }
52
53
54
55
56
57
58 #' @title Color tree's branches according to sub-clusters
59 #' @export
60 #' @aliases
61 #' colour_branches
62 #' branches_color
63 #' @description
64 #' This function is for dendrogram and hclust objects.
65 #' This function colors both the terminal leaves of a dend's cluster and the edges
66 #' leading to those leaves. The edgePar attribute of nodes will be augmented by
67 #' a new list item col.
68 #' The groups will be defined by a call to \code{\link[dendextend]{cutree}}
69 #' using the k or h parameters.
70 #'
71 #' If col is a color vector with a different length than the number of clusters
72 #' (k) - then a recycled color vector will be used.
73 #'
74 #' @details
75 #' If \code{groupLabels=TRUE} then numeric group labels will be added
76 #' to each cluster. If a vector is supplied then these entries will be used as
77 #' the group labels. If a function is supplied then it will be passed a
78 #' numeric vector of groups (e.g. 1:5) and must return the formatted group
79 #' labels.
80 #'
81 #' If the \link{labels} of the dendrogram are NOT character (but, for example
82 #' integers) - they are coerced into character. This step is essential for the
83 #' proper operation of the function. A dendrogram labels might happen to be
84 #' integers if they are based on an \link{hclust} performed on a \link{dist}
85 #' of an object without \link{rownames}.
86 #'
87 #'
88 #' @param dend A \code{dendrogram} or \code{hclust} tree object
89 #' @param k number of groups (passed to \code{\link[dendextend]{cutree}})
90 #' @param h height at which to cut tree (passed to \code{\link[dendextend]{cutree}})
91 #' @param col Function or vector of Colors. By default it tries to use
92 #' \link[colorspace]{rainbow_hcl} from the \code{colorspace} package.
93 #' (with parameters c=90 and l=50). If \code{colorspace} is not available,
94 #' It will fall back on the \link{rainbow} function.
95 #' @param groupLabels If TRUE add numeric group label - see Details for options
96 #' @param clusters an integer vector of clusters. This is passed to \link{branches_attr_by_clusters}.
97 #' This HAS to be of the same length as the number of leaves.
98 #' Items that belong to no cluster should get the value 0.
99 #' The vector should be of the same order as that of the labels in the dendrogram.
100 #' If you create the clusters from something like \link{cutree} you would first
101 #' need to use \link{order.dendrogram} on it, before using it in the function.
102 #' @param warn logical (default from dendextend_options("warn") is FALSE).
103 #' Set if warning are to be issued, it is safer to keep this at TRUE,
104 #' but for keeping the noise down, the default is FALSE.
105 #' @param ... ignored.
106 #' @return a tree object of class \link{dendrogram}.
107 #' @author Tal Galili, extensively based on code by Gregory Jefferis
108 #' @source
109 #' This function is a derived work from the \code{\link[dendroextras]{color_clusters}}
110 #' function, with some ideas from the \code{\link[dendroextras]{slice}} function -
111 #' both are from the {\pkg{dendroextras}} package by jefferis.
112 #'
113 #' It extends it by using \link[dendextend]{cutree.dendrogram} - allowing
114 #' the function to work for trees that hclust can not handle
115 #' (unbranched and non-ultrametric trees).
116 #' Also, it allows REPEATED cluster color assignments to branches on to
117 #' the same tree. Something which the original function was not able to handle.
118 #'
119 #' @seealso \code{\link[dendextend]{cutree}},\code{\link{dendrogram}},
120 #' \code{\link{hclust}}, \code{\link{labels_colors}},
121 #' \code{\link{branches_attr_by_clusters}}, \link{get_leaves_branches_col},
122 #' \link{color_labels}
123 #'
124 #' @examples
125 #'
126 #' \dontrun{
127 #' par(mfrow = c(1, 2))
128 #' dend <- USArrests %>%
129 #' dist() %>%
130 #' hclust(method = "ave") %>%
131 #' as.dendrogram()
132 #' d1 <- color_branches(dend, k = 5, col = c(3, 1, 1, 4, 1))
133 #' plot(d1) # selective coloring of branches :)
134 #' d2 <- color_branches(dend, 5)
135 #' plot(d2)
136 #'
137 #' par(mfrow = c(1, 2))
138 #' d1 <- color_branches(dend, 5, col = c(3, 1, 1, 4, 1), groupLabels = TRUE)
139 #' plot(d1) # selective coloring of branches :)
140 #' d2 <- color_branches(dend, 5, groupLabels = TRUE)
141 #' plot(d2)
142 #'
143 #' par(mfrow = c(1, 3))
144 #' d5 <- color_branches(dend, 5)
145 #' plot(d5)
146 #' d5g <- color_branches(dend, 5, groupLabels = TRUE)
147 #' plot(d5g)
148 #' d5gr <- color_branches(dend, 5, groupLabels = as.roman)
149 #' plot(d5gr)
150 #'
151 #' par(mfrow = c(1, 1))
152 #'
153 #' # messy - but interesting:
154 #' dend_override <- color_branches(dend, 2, groupLabels = as.roman)
155 #' dend_override <- color_branches(dend_override, 4, groupLabels = as.roman)
156 #' dend_override <- color_branches(dend_override, 7, groupLabels = as.roman)
157 #' plot(dend_override)
158 #'
159 #' d5 <- color_branches(dend = dend[[1]], k = 5)
160 #'
161 #'
162 #' library(dendextend)
163 #' data(iris, envir = environment())
164 #' d_iris <- dist(iris[, -5])
165 #' hc_iris <- hclust(d_iris)
166 #' dend_iris <- as.dendrogram(hc_iris)
167 #' dend_iris <- color_branches(dend_iris, k = 3)
168 #'
169 #' library(colorspace)
170 #' labels_colors(dend_iris) <-
171 #' rainbow_hcl(3)[sort_levels_values(
172 #' as.numeric(iris[, 5])[order.dendrogram(dend_iris)]
173 #' )]
174 #'
175 #' plot(dend_iris,
176 #' main = "Clustered Iris dataset",
177 #' sub = "labels are colored based on the true cluster"
178 #' )
179 #'
180 #'
181 #'
182 #' # cutree(dend_iris,k=3, order_clusters_as_data=FALSE,
183 #' # try_cutree_hclust=FALSE)
184 #' # cutree(dend_iris,k=3, order_clusters_as_data=FALSE)
185 #'
186 #' library(colorspace)
187 #'
188 #' data(iris, envir = environment())
189 #' d_iris <- dist(iris[, -5])
190 #' hc_iris <- hclust(d_iris)
191 #' labels(hc_iris) # no labels, because "iris" has no row names
192 #' dend_iris <- as.dendrogram(hc_iris)
193 #' is.integer(labels(dend_iris)) # this could cause problems...
194 #'
195 #' iris_species <- rev(levels(iris[, 5]))
196 #' dend_iris <- color_branches(dend_iris, k = 3, groupLabels = iris_species)
197 #' is.character(labels(dend_iris)) # labels are no longer "integer"
198 #'
199 #' # have the labels match the real classification of the flowers:
200 #' labels_colors(dend_iris) <-
201 #' rainbow_hcl(3)[sort_levels_values(
202 #' as.numeric(iris[, 5])[order.dendrogram(dend_iris)]
203 #' )]
204 #'
205 #' # We'll add the flower type
206 #' labels(dend_iris) <- paste(as.character(iris[, 5])[order.dendrogram(dend_iris)],
207 #' "(", labels(dend_iris), ")",
208 #' sep = ""
209 #' )
210 #'
211 #' dend_iris <- hang.dendrogram(dend_iris, hang_height = 0.1)
212 #'
213 #' # reduce the size of the labels:
214 #' dend_iris <- assign_values_to_leaves_nodePar(dend_iris, 0.5, "lab.cex")
215 #'
216 #' par(mar = c(3, 3, 3, 7))
217 #' plot(dend_iris,
218 #' main = "Clustered Iris dataset
219 #' (the labels give the true flower species)",
220 #' horiz = TRUE, nodePar = list(cex = .007)
221 #' )
222 #' legend("topleft", legend = iris_species, fill = rainbow_hcl(3))
223 #' a <- dend_iris[[1]]
224 #' dend_iris1 <- color_branches(a, k = 3)
225 #' plot(dend_iris1)
226 #'
227 #' # str(dendrapply(d2, unclass))
228 #' # unclass(d1)
229 #'
230 #' c(1:5) %>% # take some data
231 #' dist() %>% # calculate a distance matrix,
232 #' # on it compute hierarchical clustering using the "average" method,
233 #' hclust(method = "single") %>%
234 #' as.dendrogram() %>%
235 #' color_branches(k = 3) %>%
236 #' plot() # nice, returns the tree as is...
237 #'
238 #'
239 #' # Example of the "clusters" parameter
240 #' par(mfrow = c(1, 2))
241 #' dend <- c(1:5) %>%
242 #' dist() %>%
243 #' hclust() %>%
244 #' as.dendrogram()
245 #' dend %>%
246 #' color_branches(k = 3) %>%
247 #' plot()
248 #' dend %>%
249 #' color_branches(clusters = c(1, 1, 2, 2, 3)) %>%
250 #' plot()
251 #'
252 #'
253 #' # another example, based on the question here:
254 #' # https://stackoverflow.com/q/45432271/256662
255 #'
256 #'
257 #' library(cluster)
258 #' set.seed(999)
259 #' iris2 <- iris[sample(x = 1:150, size = 50, replace = F), ]
260 #' clust <- diana(iris2)
261 #' dend <- as.dendrogram(clust)
262 #'
263 #' temp_col <- c("red", "blue", "green")[as.numeric(iris2$Species)]
264 #' temp_col <- temp_col[order.dendrogram(dend)]
265 #' temp_col <- factor(temp_col, unique(temp_col))
266 #'
267 #' library(dendextend)
268 #' dend %>%
269 #' color_branches(clusters = as.numeric(temp_col), col = levels(temp_col)) %>%
270 #' set("labels_colors", as.character(temp_col)) %>%
271 #' plot()
272 #' }
273 #'
274 color_branches <- function(dend, k = NULL, h = NULL, col, groupLabels = NULL,
275 clusters,
276 warn = dendextend_options("warn"), ...) {
277
278 # This function makes sure a col vector is produced from various outputs.
279 get_col <- function(col, k) {
280 if (is.function(col)) {
281 col <- col(k)
282 } else {
283 if (length(col) < k) {
284 # stop("Must give the same number of colors as number of clusters")
285 warning("Length of color vector was shorter than the number of clusters - color vector was recycled")
286 col <- rep(col, length.out = k)
287 }
288 if (length(col) > k) {
289 # stop("Must give the same number of colors as number of clusters")
290 warning("Length of color vector was longer than the number of clusters - first k elements are used")
291 col <- col[seq_len(k)]
292 }
293 }
294 return(col)
295 }
296
297
298
299 # col <- colorspace::rainbow_hcl
300 if (missing(col)) col <- rainbow_fun
301
302
303 if (!missing(clusters)) {
304 if (!missing(k)) warning("Both the parameters 'cluster' and 'k' are not missing - k is ignored.")
305
306 if (length(clusters) != nleaves(dend)) {
307 warning("'clusters' is not of the same length as the number of labels. The dend is returned as is.")
308 return(dend)
309 }
310
311 u_clusters <- unique(clusters)
312 k <- length(u_clusters)
313 col <- get_col(col, k)
314 # col[u_clusters==0] <- "black"
315 return(branches_attr_by_clusters(dend, clusters,
316 values = col, attr = "col",
317 branches_changed_have_which_labels = "all"
318 ))
319 }
320
321
322
323 # Make sure the function works when the labels of the dend are not all unique
324 old_labels <- labels(dend)
325 labels_arent_unique <- !all_unique(old_labels)
326 if (labels_arent_unique) {
327 if (warn) warning("Your dend labels are NOT unique!\n This may cause an un expected issue with the color of the branches.\n Hence, your labels were temporarily turned unique (and then fixed as they were before).")
328 labels(dend) <- seq_along(old_labels)
329 }
330
331
332 if (is.null(k) & is.null(h)) {
333 if (warn) warning("k (number of clusters) is missing, using the dend size as a default")
334 k <- nleaves(dend)
335 }
336
337 if (!is.dendrogram(dend) && !is.hclust(dend)) stop("dend needs to be either a dendrogram or an hclust object")
338 g <- dendextend::cutree(dend, k = k, h = h, order_clusters_as_data = FALSE)
339 if (is.hclust(dend)) dend <- as.dendrogram(dend)
340
341 k <- max(g)
342
343 # For when we have flat dends
344 if (k == 0L) {
345 if (warn) warning("dend has only one level - returning the dendrogram with no colors.")
346 return(dend)
347 }
348
349 col <- get_col(col, k)
350
351 if (!is.null(groupLabels)) {
352 if (length(groupLabels) == 1) {
353 if (is.function(groupLabels)) {
354 groupLabels <- groupLabels(seq.int(length.out = k))
355 } else if (is.logical(groupLabels)) {
356 if (groupLabels) {
357 groupLabels <- seq.int(length.out = k)
358 } else {
359 groupLabels <- NULL
360 }
361 }
362 }
363 if (!is.null(groupLabels) && length(groupLabels) != k) {
364 stop("Must give same number of group labels as clusters")
365 }
366 }
367
368 addcol <- function(dend_node, col) {
369 if (is.null(attr(dend_node, "edgePar"))) {
370 attr(dend_node, "edgePar") <- list(col = col)
371 } else {
372 attr(dend_node, "edgePar")[["col"]] <- col
373 # within(attr(dend_node, "edgePar"),
374 # {col=col})
375 # this way it doesn't erase other nodePar values (if they exist)
376 }
377 unclass(dend_node)
378 }
379
380 descendTree <- function(sd) {
381 groupsinsubtree <- unique(g[labels(sd)])
382 if (length(groupsinsubtree) > 1) {
383 # keep descending
384 for (i in seq(sd)) {
385 sd[[i]] <- descendTree(sd[[i]])
386 }
387 } else {
388 # else assign Colors
389 # sd=dendrapply(sd,addcol,col[groupsinsubtree],groupsinsubtree)
390 sd <- dendrapply(sd, addcol, col[groupsinsubtree])
391 if (!is.null(groupLabels)) {
392 attr(sd, "edgetext") <- groupLabels[groupsinsubtree]
393 attr(sd, "edgePar") <- c(attr(sd, "edgePar"), list(p.border = col[groupsinsubtree]))
394 # attr(sd,'edgePar')[["p.border"]]=col[groupsinsubtree]
395 }
396 }
397 unclass(sd)
398 }
399
400 if (!is.character(labels(dend))) labels(dend) <- as.character(labels(dend))
401 dend <- descendTree(dend)
402 class(dend) <- "dendrogram"
403
404 # If we previously had "uniqified" the labels, they should now be "fixed back" before returning the dend.
405 if (labels_arent_unique) labels(dend) <- old_labels
406
407 dend
408 }
409
410 # str(unclass(d2))
411 # plot(dend)
412
413 # nice idea - make this compatible with colour/color
414 #' @export
415 colour_branches <- color_branches
416
417 #' @export
418 branches_color <- color_branches
419
420
421
422
423
424
425
426
427
428
429
430 if (F) {
431 data(iris, envir = environment())
432 d_iris <- dist(iris[1:4, -5], method = "man")
433 hc_iris <- hclust(d_iris)
434 labels(hc_iris) # no labels, because "iris" has no row names
435 dend_iris <- as.dendrogram(hc_iris)
436 plot(dend_iris)
437
438 labels_colors(dend_iris)[2] <- 2
439 is.integer(labels(dend_iris)) # this could cause problems...
440
441 # than add this function to color_labels, add a labels parameter - and have it override everything else!
442 plot(color_labels_by_labels(dend_iris, c("1", "4"), c(2, 3, 5), warn = TRUE))
443
444 # Maybe I should create color_labels_by_kh!!!
445 color_labels
446 # add a "warn" parameter
447 }
448
449 color_labels_by_labels <- function(dend, labels, col, warn = dendextend_options("warn"), ...) {
450 dend_labels <- labels(dend)
451 dend_col <- labels_colors(dend)
452
453 ss_labels_to_color <- dend_labels %in% labels
454
455 # recycle colors if they are shorter than the labels.
456 rep_col <- rep_len(col, sum(ss_labels_to_color))
457 if (length(rep_col) != length(col)) {
458 if (warn) warning("The length of 'col' is different than that of the labels in the dend. 'col' recycled to fit the length.")
459 col <- rep_col
460 }
461
462 # fix vector of labels colors
463 dend_col[ss_labels_to_color] <- col
464 # fix labels colors in the dend
465 labels_colors(dend) <- dend_col
466
467 return(dend)
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487 #' @title Color dend's labels according to sub-clusters
488 #' @export
489 #' @aliases
490 #' colour_labels
491 #' @description
492 #' This function is for dendrogram and hclust objects.
493 #' This function colors tree's labels.
494 #'
495 #' The groups will be defined by a call to \code{\link[dendextend]{cutree}}
496 #' using the k or h parameters.
497 #'
498 #' If col is a color vector with a different length than the number of clusters
499 #' (k) - then a recycled color vector will be used.
500 #'
501 #'
502 #'
503 #' @param dend A \code{dendrogram} or \code{hclust} tree object
504 #' @param k number of groups (passed to \code{\link[dendextend]{cutree}})
505 #' @param h height at which to cut tree (passed to \code{\link[dendextend]{cutree}})
506 #' @param col Function or vector of Colors. By default it tries to use
507 #' \link[colorspace]{rainbow_hcl} from the \code{colorspace} package.
508 #' (with parameters c=90 and l=50). If \code{colorspace} is not available,
509 #' It will fall back on the \link{rainbow} function.
510 #' @param labels character vecotor. If not missing, it overrides k and h,
511 #' and simply colors these labels in the tree based on "col" parameter.
512 #' @param warn logical (default from dendextend_options("warn") is FALSE).
513 #' Set if warning are to be issued, it is safer to keep this at TRUE,
514 #' but for keeping the noise down, the default is FALSE.
515 #' (in case h/k/labels are not supplied, or if col is too short)
516 #' @param ... ignored.
517 #' @return a tree object of class dendrogram.
518 #' @source
519 #' This function is in the style of \code{\link{color_branches}}, and
520 #' based on \code{\link{labels_colors}}.
521 #' @seealso \code{\link[dendextend]{cutree}},\code{\link{dendrogram}},
522 #' \code{\link{hclust}}, \code{\link{labels_colors}}, \code{\link{color_branches}},
523 #' \link{assign_values_to_leaves_edgePar}
524 #' @examples
525 #'
526 #' \dontrun{
527 #' hc <- hclust(dist(USArrests), "ave")
528 #' dend <- as.dendrogram(hc)
529 #' dend <- color_labels(dend, 5, col = c(3, 1, 1, 4, 1))
530 #' dend <- color_branches(dend, 5, col = c(3, 1, 1, 4, 1))
531 #' plot(dend) # selective coloring of branches AND labels :)
532 #'
533 #' # coloring some labels, based on label names:
534 #' dend <- color_labels(dend, col = "red", labels = labels(dend)[c(4, 16)])
535 #' plot(dend) # selective coloring of branches AND labels :)
536 #'
537 #' d5 <- color_branches(dend, 5)
538 #' plot(d5)
539 #' d5g <- color_branches(dend, 5, groupLabels = TRUE)
540 #' plot(d5g)
541 #' d5gr <- color_branches(dend, 5, groupLabels = as.roman)
542 #' plot(d5gr)
543 #' }
544 #'
545 color_labels <- function(dend, k = NULL, h = NULL, labels, col, warn = dendextend_options("warn"), ...) {
546 if (!missing(labels)) {
547 return(color_labels_by_labels(dend = dend, labels = labels, col = col, warn = warn, ...))
548 }
549
550
551 if (missing(col)) col <- rainbow_fun
552
553 if (!is.dendrogram(dend) && !is.hclust(dend)) stop("dend needs to be either a dendrogram or an hclust object")
554
555 if (missing(k) & missing(h)) {
556 # k = nleaves(dend)
557 if (warn) warning("Neither k nor h were supplied - coloring all leaves based on 'col'.")
558 if (is.function(col)) col <- col(nleaves(dend)) # if we also didn't get any col, fill the colors with rainbow...
559 labels_colors(dend) <- col
560 return(dend)
561 }
562
563 g <- dendextend::cutree(dend, k = k, h = h, order_clusters_as_data = FALSE)
564 if (is.hclust(dend)) dend <- as.dendrogram(dend)
565
566 k <- max(g)
567 if (is.function(col)) {
568 col <- col(k)
569 } else {
570 if (length(col) < k) {
571 # stop("Must give the same number of colors as number of clusters")
572 if (warn) warning("Length of color vector was shorter than the number of clusters - color vector was recycled")
573 col <- rep(col, length.out = k)
574 }
575 if (length(col) > k) {
576 # stop("Must give the same number of colors as number of clusters")
577 if (warn) warning("Length of color vector was longer than the number of clusters - first k elements are used")
578 col <- col[seq_len(k)]
579 }
580 }
581
582
583 labels_colors(dend) <- col[g]
584
585 return(dend)
586 }
587
588
589 # nice idea - make this compatible with colour/color
590 colour_labels <- color_labels
591
592
593
594 lty_branches <- function(dend, k = NULL, h = NULL,
595 lty,
596 ...) {
597 clusters <- cutree(dend, k = k, h = h)[order.dendrogram(dend)]
598 if (missing(lty)) lty <- clusters
599 dend <- branches_attr_by_clusters(dend, clusters,
600 values = lty, attr = "lty",
601 branches_changed_have_which_labels = c("all")
602 )
603 dend
604 }
605 # dend <- USArrests %>% dist %>% hclust(method = "ave") %>% as.dendrogram
606 # dend %>% lty_branches(k = 3) %>% plot
607
608
609
610
611 #' @title Return the leaf Colors of a dendrogram
612 #' @description The returned Colors will be in dendrogram order.
613 #' @param d the dendrogram
614 #' @param col_to_return Character scalar - kind of Color attribute to return
615 #' @return named character vector of Colors, NA_character_ where missing
616 #' @author jefferis
617 #' @export
618 #' @aliases leaf_colors
619 #' @seealso \code{\link[dendroextras]{slice},\link{color_branches}}
620 #' @examples
621 #' dend <- USArrests %>%
622 #' dist() %>%
623 #' hclust(method = "ave") %>%
624 #' as.dendrogram()
625 #' d5 <- color_branches(dend, 5)
626 #' leaf_Colors(d5)
627 leaf_Colors <- function(d, col_to_return = c("edge", "node", "label")) {
628 if (!inherits(d, "dendrogram")) stop("I need a dendrogram!")
629 col_to_return <- match.arg(col_to_return)
630 leaf_col <- function(n, col_to_return) {
631 if (is.leaf(n)) {
632 col <- switch(col_to_return,
633 edge = attr(n, "edgePar")$col,
634 node = attr(n, "nodePar")$col,
635 label = attr(n, "nodePar")$lab.col
636 )
637 if (is.null(col)) col <- NA_character_
638 structure(col, .Names = attr(n, "label"))
639 } else {
640 NULL
641 }
642 }
643
644 unlist(dendrapply(d, leaf_col, col_to_return))
645 }
646
647
648 #' @export
649 leaf_colors <- leaf_Colors
650
651
652
653
654
655
656
657 color_branches_by_clusters <- function(dend, clusters, unique_colors, order_value = FALSE, ...) {
658 if (length(clusters) != length(labels(dend))) stop("clusters must be of the same length as the labels in dend.")
659
660 if (order_value) clusters <- clusters[order.dendrogram(dend)]
661
662 clusters <- factor(clusters, levels = unique(clusters))
663 if (missing(unique_colors)) unique_colors <- levels(clusters)
664 clusters <- as.numeric(clusters)
665
666 branches_attr_by_clusters(dend, clusters = clusters, values = unique_colors)
667 }