Debian Codegraph @

R/attr_access.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 # like is.infinite but acts differently for character
21 is.infinite2 <- function(x) {
22 if (is.character(x)) {
23 return(x == "Inf")
24 }
25 # else { return(
26 is.infinite(x)
27 }
28
29
30
31
32
33
34 ### # ' @aliases
35 ### # ' set_leaves_attr
36 ### # ' @usage
37 ### # ' get_leaves_attr(dend, labels = TRUE,...)
38 ### # '
39 ### # ' set_leaves_attr(dend, ...) <- value
40
41 #' @title Get/set attributes of dendrogram's leaves
42 #' @description Get/set attributes of dendrogram's leaves
43 #' @export
44 #' @param dend a dendrogram object
45 #' @param attribute character scalar of the attribute (\code{attr})
46 #' we wish to get/set from the leaves
47 #' @param simplify logical. If TRUE (default), then the return vector is
48 #' after using \code{unlist} on it.
49 #' @param ... not used
50 #' @source Heavily inspired by the code in the
51 #' function \code{labels.dendrogram},
52 #' so credit should go to Martin Maechler.
53 #' @return
54 #' A vector (or a list) with the dendrogram's leaves attribute
55 #' @seealso \link{get_nodes_attr}, \link{nnodes},
56 #' \link{nleaves}, \link{assign_values_to_leaves_nodePar}
57 #' @examples
58 #' # define dendrogram object to play with:
59 #' hc <- hclust(dist(USArrests[1:3, ]), "ave")
60 #' dend <- as.dendrogram(hc)
61 #'
62 #' # get_leaves_attr(dend) # error :)
63 #' get_leaves_attr(dend, "label")
64 #' labels(dend, "label")
65 #' get_leaves_attr(dend, "height") # should be 0's
66 #' get_nodes_attr(dend, "height")
67 #'
68 #' get_leaves_attr(dend, "nodePar")
69 #'
70 #'
71 #' get_leaves_attr(dend, "leaf") # should be TRUE's
72 #' get_nodes_attr(dend, "leaf") # conatins NA's
73 #'
74 #'
75 #' get_leaves_attr(dend, "members") # should be 1's
76 #' get_nodes_attr(dend, "members") #
77 #'
78 #'
79 #' get_leaves_attr(dend, "members", simplify = FALSE) # should be 1's
80 get_leaves_attr <- function(dend, attribute, simplify = TRUE, ...) {
81 if (!is.dendrogram(dend)) warning("'dend' should be a dendrogram.")
82 if (missing(attribute)) stop("'attribute' parameter is missing.")
83
84 get_attr_from_leaf <- function(dend_node) {
85 if (is.leaf(dend_node)) attr(dend_node, attribute)
86 }
87
88 ret <- dendrapply(dend, get_attr_from_leaf)
89 if (simplify) ret <- unlist(ret)
90
91 return(ret)
92 }
93
94
95
96
97
98
99
100
101
102 #' @title Get nodePar of dendrogram's leaves
103 #' @description Get the nodePar attributes of dendrogram's leaves (includes pch, color, and cex)
104 #' @export
105 #' @param dend a dendrogram object
106 #' @param simplify logical (default is FALSE). If TRUE, then the return vector is
107 #' after using \code{unlist} on it.
108 #' @param ... not used
109 #' @return
110 #' A list (or a vector) with the dendrogram's leaves nodePar attribute
111 #' @seealso \link{get_nodes_attr}, \link{assign_values_to_leaves_nodePar}, \link{labels_colors}
112 #' \link{get_leaves_edgePar}
113 #' @examples
114 #' # define dendrogram object to play with:
115 #' hc <- hclust(dist(USArrests[1:3, ]), "ave")
116 #' dend <- as.dendrogram(hc)
117 #'
118 #' # get_leaves_attr(dend) # error :)
119 #' get_leaves_nodePar(dend)
120 #' labels_colors(dend) <- 1:3
121 #' get_leaves_nodePar(dend)
122 #'
123 #' dend <- assign_values_to_leaves_nodePar(dend, 2, "lab.cex")
124 #' get_leaves_nodePar(dend)
125 #'
126 #' plot(dend)
127 get_leaves_nodePar <- function(dend, simplify = FALSE, ...) {
128 if (!is.dendrogram(dend)) warning("'dend' should be a dendrogram.")
129
130 is_node_leaf <- get_nodes_attr(dend, "leaf")
131 is_node_leaf[is.na(is_node_leaf)] <- FALSE
132
133 ret <- get_nodes_attr(dend, "nodePar", simplify = FALSE)[is_node_leaf]
134 if (simplify) ret <- unlist(ret)
135
136 return(ret)
137 }
138
139
140
141
142
143
144
145
146 #' @title Get edgePar of dendrogram's leaves
147 #' @export
148 #' @description
149 #' This is helpful to get the attributes of branches of the leaves.
150 #' For example, after we use \link{color_branches}, to get the colors
151 #' of the labels to match (since getting the colors of branches to match
152 #' those of the labels can be tricky).
153 #' @param dend a dendrogram object
154 #' @param simplify logical (default is FALSE). If TRUE, then the return vector is
155 #' after using \code{unlist} on it.
156 #' @param ... not used
157 #' @return
158 #' A list (or a vector) with the dendrogram's leaves edgePar attribute
159 #' @seealso \link{get_nodes_attr}, \link{assign_values_to_leaves_nodePar}, \link{labels_colors}
160 #' \link{get_leaves_nodePar}
161 #' @examples
162 #' # define dendrogram object to play with:
163 #' hc <- hclust(dist(USArrests[1:5, ]), "ave")
164 #' dend <- as.dendrogram(hc)
165 #'
166 #' # get_leaves_edgePar(dend) # error :)
167 #' get_leaves_edgePar(dend)
168 #' dend <- color_branches(dend, k = 3)
169 #' get_leaves_edgePar(dend)
170 #' get_leaves_edgePar(dend, TRUE)
171 #'
172 #' dend <- dend %>% set("branches_lwd", c(2, 1, 2))
173 #' get_leaves_edgePar(dend)
174 #'
175 #' plot(dend)
176 get_leaves_edgePar <- function(dend, simplify = FALSE, ...) {
177 if (!is.dendrogram(dend)) warning("'dend' should be a dendrogram.")
178
179 is_node_leaf <- get_nodes_attr(dend, "leaf")
180 is_node_leaf[is.na(is_node_leaf)] <- FALSE
181
182 ret <- get_nodes_attr(dend, "edgePar", simplify = FALSE)[is_node_leaf]
183 if (simplify) ret <- unlist(ret)
184
185 return(ret)
186 }
187
188
189
190
191
192
193 #' @title Get an attribute of the branches of a dendrogram's leaves
194 #' @export
195 #' @description
196 #' This is helpful to get the attributes of branches of the leaves.
197 #' For example, after we use \link{color_branches}, to get the colors
198 #' of the labels to match (since getting the colors of branches to match
199 #' those of the labels can be tricky).
200 #' This is based on \link{get_leaves_edgePar}.
201 #'
202 #' @param dend a dendrogram object
203 #' @param attr character, the attr to get. Can be either "col", "lwd", or "lty".
204 #' @param ... not used
205 #' @return
206 #' A vector with the dendrogram's leaves nodePar attribute
207 #' @seealso \link{get_nodes_attr}, \link{assign_values_to_leaves_nodePar}, \link{labels_colors}
208 #' \link{get_leaves_nodePar}, \link{get_leaves_edgePar}
209 #' @examples
210 #' # define dendrogram object to play with:
211 #' hc <- hclust(dist(USArrests[1:5, ]), "ave")
212 #' dend <- as.dendrogram(hc)
213 #'
214 #' dend <- dend %>%
215 #' color_branches(k = 3) %>%
216 #' set("branches_lwd", c(2, 1, 2)) %>%
217 #' set("branches_lty", c(1, 2, 1))
218 #'
219 #' plot(dend)
220 #'
221 #' get_leaves_branches_attr(dend, "col")
222 #' get_leaves_branches_attr(dend, "lwd")
223 #' get_leaves_branches_attr(dend, "lty")
224 #'
225 #' labels_colors(dend) <- get_leaves_branches_attr(dend, "col")
226 #' plot(dend)
227 get_leaves_branches_attr <- function(dend, attr = c("col", "lwd", "lty"), ...) {
228 if (!is.dendrogram(dend)) warning("'dend' should be a dendrogram.")
229 attr <- match.arg(attr)
230
231 dend_leaves_edgePar <- get_leaves_edgePar(dend)
232
233 get_attr <- function(element) element[attr]
234
235 unname(unlist(lapply(dend_leaves_edgePar, get_attr)))
236 }
237
238
239
240
241
242 #' @title Get the colors of the branches of a dendrogram's leaves
243 #' @export
244 #' @description
245 #' It is useful to get the colors of branches of the leaves,
246 #' after we use \link{color_branches}, so to then match the colors of the labels
247 #' to that of the branches (since getting the colors of branches to match
248 #' those of the labels can be tricky).
249 #' This is based on \link{get_leaves_branches_attr} which is based on
250 #' \link{get_leaves_edgePar}.
251 #'
252 #' TODO: The function get_leaves_branches_col may behave oddly when extracting
253 #' colors with missing col attributes when the lwd attribute is available.
254 #' This may resolt in a vector with the wrong length (with omitted NA values).
255 #' This might need to be fixed in the future, and attention should be given to this case.
256 #'
257 #' @param dend a dendrogram object
258 #' @param ... not used
259 #' @return
260 #' A vector with the dendrogram's leaves' branches' colors
261 #' @seealso \link{get_nodes_attr}, \link{assign_values_to_leaves_nodePar}, \link{labels_colors}
262 #' \link{get_leaves_nodePar}, \link{get_leaves_edgePar}, \link{get_leaves_branches_attr}
263 #' @examples
264 #' # define dendrogram object to play with:
265 #' hc <- hclust(dist(USArrests[1:5, ]), "ave")
266 #' dend <- as.dendrogram(hc)
267 #'
268 #' par(mfrow = c(1, 2), mar = c(5, 2, 1, 0))
269 #' dend <- dend %>%
270 #' color_branches(k = 3) %>%
271 #' set("branches_lwd", c(2, 1, 2)) %>%
272 #' set("branches_lty", c(1, 2, 1))
273 #'
274 #' plot(dend)
275 #'
276 #' labels_colors(dend) <- get_leaves_branches_col(dend)
277 #' plot(dend)
278 get_leaves_branches_col <- function(dend, ...) {
279 get_leaves_branches_attr(dend, attr = "col", ...)
280 }
281
282
283
284 #' @title Get attributes of dendrogram's nodes
285 #' @export
286 #' @description
287 #' Allows easy access to attributes of branches and/or leaves, with option
288 #' of returning a vector with/withough NA's (for marking the missing attr value)
289 #'
290 #' @param dend a dendrogram object
291 #' @param attribute character scalar of the attribute (\code{attr})
292 #' we wish to get from the nodes
293 #' @param id integer vector. If given - only the attr of these nodes id will be returned (via depth first search)
294 #' @param include_leaves logical. Should leaves attributes be included as well?
295 #' @param include_branches logical. Should non-leaf (branch node)
296 #' attributes be included as well?
297 #' @param simplify logical (default is TRUE). should the result be simplified
298 #' to a vector (using \link{simplify2array} ) if possible? If it is not possible
299 #' it will return a matrix. When FALSE, a list is returned.
300 #' @param na.rm logical. Should NA attributes be REMOVED from the resulting vector?
301 #' @param ... not used
302 #' @source Heavily inspired by the code in the
303 #' function \code{labels.dendrogram},
304 #' so credit should go to Martin Maechler.
305 #' @return
306 #' A vector with the dendrogram's nodes attribute. If an attribute is missing
307 #' from some nodes, it will return NA in that vector.
308 #' @seealso \link{get_leaves_attr}, \link{nnodes}, \link{nleaves}
309 #' @examples
310 #' # define dendrogram object to play with:
311 #' hc <- hclust(dist(USArrests[1:3, ]), "ave")
312 #' dend <- as.dendrogram(hc)
313 #'
314 #' # get_leaves_attr(dend) # error :)
315 #' get_leaves_attr(dend, "label")
316 #' labels(dend, "label")
317 #' get_leaves_attr(dend, "height") # should be 0's
318 #' get_nodes_attr(dend, "height")
319 #'
320 #'
321 #' get_leaves_attr(dend, "leaf") # should be TRUE's
322 #' get_nodes_attr(dend, "leaf") # conatins NA's
323 #'
324 #'
325 #' get_leaves_attr(dend, "members") # should be 1's
326 #' get_nodes_attr(dend, "members", include_branches = FALSE, na.rm = TRUE) #
327 #' get_nodes_attr(dend, "members") #
328 #' get_nodes_attr(dend, "members", simplify = FALSE)
329 #' get_nodes_attr(dend, "members", include_leaves = FALSE, na.rm = TRUE) #
330 #'
331 #' get_nodes_attr(dend, "members", id = c(1, 3), simplify = FALSE)
332 #' get_nodes_attr(dend, "members", id = c(1, 3)) #
333 #'
334 #'
335 #' hang_dend <- hang.dendrogram(dend)
336 #' get_leaves_attr(hang_dend, "height") # no longer 0!
337 #' get_nodes_attr(hang_dend, "height") # does not include any 0s!
338 #'
339 #' # does not include leaves values:
340 #' get_nodes_attr(hang_dend, "height", include_leaves = FALSE)
341 #' # remove leaves values all together:
342 #' get_nodes_attr(hang_dend, "height", include_leaves = FALSE, na.rm = TRUE)
343 #' \dontrun{
344 #' library(microbenchmark)
345 #' # get_leaves_attr is twice faster than get_nodes_attr
346 #' microbenchmark(
347 #' get_leaves_attr(dend, "members"), # should be 1's
348 #' get_nodes_attr(dend, "members", include_branches = FALSE, na.rm = TRUE)
349 #' )
350 #' }
351 #'
352 get_nodes_attr <- function(dend, attribute,
353 id,
354 include_leaves = TRUE,
355 include_branches = TRUE,
356 simplify = TRUE,
357 na.rm = FALSE, ...) {
358 if (!is.dendrogram(dend)) warning("'dend' should be a dendrogram.")
359 if (missing(attribute)) stop("'attribute' parameter is missing.")
360
361 #### for some reason, this doesn't work:
362 # get_attr_from_node <- function(dend_node) {
363 # i_attr <- attr(dend_node, attribute)
364 # ifelse(is.null(i_attr), NA, i_attr)
365 # }
366 #
367 # return((dendrapply(dend, get_attr_from_node)))
368
369
370 # dend_attr <- rep(NA, nnodes(dend))
371 # empty_list <- vector("list", nnodes(dend))
372 empty_list <- as.list(rep(NA, nnodes(dend)))
373 dend_attr <- empty_list
374 missing_id <- missing(id)
375
376
377 # this function is used to modify dend_attr. What it returns is not important.
378 i_node <- 0
379 get_attr_from_node <- function(dend_node) {
380 i_node <<- i_node + 1
381
382 # if we have id's and this is not it - we can skip it...
383 # FALSE & NULL # fails
384 # FALSE && NULL # works...
385 if (!missing_id && !(i_node %in% id)) {
386 return(invisible())
387 }
388
389
390 # if we should not include_leaves, then we skip when a leaf is encountered.
391 if (!include_leaves && is.leaf(dend_node)) {
392 return(NULL)
393 }
394 if (!include_branches && !is.leaf(dend_node)) {
395 return(NULL)
396 }
397
398 i_attr <- attr(dend_node, attribute)
399 if (!is.null(i_attr)) dend_attr[[i_node]] <<- i_attr
400 return(invisible())
401 }
402 dendrapply(dend, get_attr_from_node)
403
404 # as.vector is to remove all classes of the na.omit
405 # thank you Prof. Brian Ripley http://tolstoy.newcastle.edu.au/R/e2/devel/07/01/1965.html
406 if (simplify) dend_attr <- simplify2array(dend_attr)
407
408 if (na.rm) dend_attr <- as.vector(na.omit(dend_attr))
409
410
411 if (dendextend_options("warn") && identical(dend_attr, simplify2array(empty_list))) warning("It seems that the attribute '", attribute, "' does not exist - returning NA.")
412
413 # TODO: this could probably be more optimized - say, by looking only at the above mentioned id's
414 # and not create all of the vector and only then take a subset.
415 # But for now, I think this is more maintainable...
416 if (!missing_id) {
417 dend_attr <- dend_attr[id]
418 }
419
420 return(dend_attr)
421 }
422
423
424
425
426
427
428
429
430
431
432
433 # ' @export
434 #' @title recursivly apply a function on a list
435 #' @description
436 #' recursivly apply a function on a list - and returns the output as a list,
437 #' following the naming convention in the \code{plyr} package
438 #' the big difference between this and rapply is that this will also apply
439 #' the function on EACH element of the list, even if it's not a "terminal node"
440 #' inside the list tree.
441 #' An attribute is added to indicate if the value returned is
442 #' from a branch or a leaf.
443 #' @param x a list.
444 #' @param FUN a function to apply on each element of the list
445 #' @param add_notation logical. Should each node be
446 #' added a "position_type" attribute, stating if it is a "Branch" or a "Leaf".
447 #' @param ... not used.
448 #' @return a list with ALL of the nodes (from the original "x" list),
449 #' that FUN was applied on.
450 #'
451 #' @examples
452 #' \dontrun{
453 #' x <- list(1)
454 #' x
455 #' rllply(x, function(x) {
456 #' x
457 #' }, add_notation = TRUE)
458 #'
459 #' x <- list(1, 2, list(31))
460 #' x
461 #' rllply(x, function(x) {
462 #' x
463 #' }, add_notation = TRUE)
464 #' # the first element is the entire tree
465 #' # after FUN was applied to its root element.
466 #'
467 #' hc <- hclust(dist(USArrests[1:4, ]), "ave")
468 #' dend <- as.dendrogram(hc)
469 #' rllply(dend, function(x) {
470 #' attr(x, "height")
471 #' })
472 #' rllply(dend, function(x) {
473 #' attr(x, "members")
474 #' })
475 #' }
476 rllply <- function(x, FUN, add_notation = FALSE, ...) {
477 if (is.list(x)) {
478 output <- list()
479 for (i in seq_len(length(x)))
480 {
481 output[[i]] <- list(rllply(x[[i]], FUN, ...))
482 if (add_notation) attr(output[[i]][[1]], "position_type") <- "Branch"
483 }
484 output <- list(FUN(x, ...), output)
485 } else {
486 output <- FUN(x, ...)
487 if (add_notation) attr(output, "position_type") <- "Leaf"
488 }
489 return(output)
490 }
491
492
493
494
495 # ' @aliases
496 # ' old_get_branches_heights
497
498
499 #' @title Get height attributes from a dendrogram
500 #' @description Get height attributes of a dendrogram's branches
501 #' @export
502 #' @param dend a dendrogram.
503 #' @param sort logical. Should the heights be sorted?
504 #' @param decreasing logical. Should the sort be increasing or decreasing? Not available for partial sorting.
505 #' @param include_leaves logical (FALSE). Should the output include the leaves value (0's).
506 #' @param ... not used.
507 #' @return
508 #' a vector of the dendrogram's nodes heights (excluding leaves).
509 #'
510 #' @examples
511 #'
512 #' hc <- hclust(dist(USArrests[1:4, ]), "ave")
513 #' dend <- as.dendrogram(hc)
514 #' get_branches_heights(dend)
515 get_branches_heights <- function(dend, sort = TRUE, decreasing = FALSE, include_leaves = FALSE, ...) {
516 # height <- unlist(rllply(dend, function(x){attr(x, "height")}))
517 # height <- get_nodes_attr(dend, "height")
518 # height <- height[height != 0] # include only the non zero values
519
520 height <- get_nodes_attr(dend, "height", include_leaves = include_leaves, na.rm = TRUE)
521 if (sort) height <- sort(height, decreasing = decreasing) # sort the height
522
523 return(height)
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537 #' @title Hang dendrogram leaves
538 #' @export
539 #' @description
540 #' Adjust the height attr in all of the dendrogram leaves so that
541 #' the tree will hang. This is similar to as.dendrogram(hclust, hang=0.1)
542 #' Only that it now works on other object than hclust turned into a dendrogram.
543 #' For example, this allows us to hang non-binary trees.
544 #'
545 #' @param dend a dendrogram object
546 #' @param hang The fraction of the plot height by which labels should hang below
547 #' the rest of the plot. A negative value will cause the labels to
548 #' hang down from 0.
549 #' @param hang_height is missing, then using "hang". If a number is given,
550 #' it overrides "hang" (except if "hang" is negative)
551 #' @param ... not used
552 #' @return
553 #' A dendrogram, after adjusting the height attr in all of its leaves,
554 #' so that the tree will hang.
555 #' @source
556 #' Noticing that as.dendrogram has a "hang" parameter was thanks to Enrique Ramos's answer here::
557 #' \url{https://stackoverflow.com/questions/17088136/plot-horizontal-dendrogram-with-hanging-leaves-r}
558 #' @examples
559 #'
560 #' # define dendrogram object to play with:
561 #' hc <- hclust(dist(USArrests[1:5, ]), "ave")
562 #' dend <- as.dendrogram(hc)
563 #'
564 #' par(mfrow = c(1, 2))
565 #' plot(hang.dendrogram(dend))
566 #' plot(hc)
567 #' # identical(as.dendrogram(hc, hang = 0.1), hang.dendrogram(dend, hang = 0.1))
568 #' # TRUE!!
569 #'
570 #'
571 #' par(mfrow = c(1, 4))
572 #'
573 #' plot(dend)
574 #' plot(hang.dendrogram(dend, hang = 0.1))
575 #' plot(hang.dendrogram(dend, hang = 0))
576 #' plot(hang.dendrogram(dend, hang = -0.1))
577 #'
578 #' par(mfrow = c(1, 1))
579 #' plot(hang.dendrogram(dend), horiz = TRUE)
580 hang.dendrogram <- function(dend, hang = 0.1, hang_height, ...) {
581 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
582
583
584 # get_heights.dendrogram
585 if (missing(hang_height)) hang_height <- attr(dend, "height") * hang
586
587 fix_height_attr_per_leaf <- function(dend_node) {
588 if (!is.leaf(dend_node)) {
589 dend_node_height <- attr(dend_node, "height")
590
591 for (i_nodes in seq_len(length(dend_node))) {
592 if (is.leaf(dend_node[[i_nodes]])) {
593 if (hang < 0) {
594 attr(dend_node[[i_nodes]], "height") <- 0
595 } else {
596 attr(dend_node[[i_nodes]], "height") <- dend_node_height - hang_height
597 }
598
599 dend_node[[i_nodes]] <- unclass(dend_node[[i_nodes]]) # makes sure we don't inherent any classes...
600 } else {
601 dend_node[[i_nodes]] <-
602 fix_height_attr_per_leaf(dend_node[[i_nodes]])
603 }
604 }
605 }
606 return(unclass(dend_node))
607 }
608
609 dend <- fix_height_attr_per_leaf(dend)
610 class(dend) <- "dendrogram"
611 return(dend)
612 }
613
614
615
616
617
618 #' @title Get height attributes from a dendrogram's children
619 #' @description Get height attributes from a dendrogram's children nodes
620 #' @export
621 #' @param dend a dendrogram.
622 #' @param ... not used.
623 #' @return
624 #' a vector of the heights of a dendrogram's current node's
625 #' (first level) children.
626 #'
627 #' @seealso
628 #' \link{get_branches_heights}
629 #' @examples
630 #'
631 #' hc <- hclust(dist(USArrests[1:4, ]), "ave")
632 #' dend <- as.dendrogram(hc)
633 #' get_childrens_heights(dend)
634 get_childrens_heights <- function(dend, ...) {
635 sapply(dend, function(x) {
636 attr(x, "height")
637 })
638 }
639
640
641
642 #' @title Rank branches' heights
643 #' @export
644 #' @description
645 #' Adjust the height attr in all of the dendrogram nodes so that
646 #' the tree will have a distance of 1 unit between each parent/child nodes.
647 #' It can be thought of as ranking the branches between themselves.
648 #'
649 #' This is intended for easier comparison of the topology of two trees.
650 #'
651 #' Notice that this function changes the height of all the leaves into 0,
652 #' thus erasing the effect of \link{hang.dendrogram} (which should be run
653 #' again, if that is the visualization you are intereted in).
654 #'
655 #' @param dend a dendrogram object
656 #' @param diff_height Numeric scalar (1). Affects the difference in height
657 #' between two branches.
658 #' @param ... not used
659 #' @seealso
660 #' \link{get_branches_heights}, \link{get_childrens_heights},
661 #' \link{hang.dendrogram}, \link{tanglegram}
662 #'
663 #' @return
664 #' A dendrogram, after adjusting the height attr in all of its branches.
665 #'
666 #' @examples
667 #'
668 #' # define dendrogram object to play with:
669 #' dend <- USArrests[1:5, ] %>%
670 #' dist() %>%
671 #' hclust() %>%
672 #' as.dendrogram()
673 #'
674 #' par(mfrow = c(1, 3))
675 #'
676 #' plot(dend)
677 #' plot(rank_branches(dend))
678 #' plot(hang.dendrogram(rank_branches(dend)))
679 rank_branches <- function(dend, diff_height = 1, ...) {
680 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
681
682
683 fix_height_diff_per_branch <- function(dend_node) {
684 if (is.leaf(dend_node)) {
685 attr(dend_node, "height") <- 0
686 return(unclass(dend_node))
687 }
688 # else:
689
690 # go through and fix all of the nodes beneath this one:
691 for (i_nodes in seq_len(length(dend_node))) {
692 dend_node[[i_nodes]] <- Recall(dend_node[[i_nodes]])
693 }
694
695 # now we can fix the current height, by finding the height of
696 # all son-branches and increasing the height of the maximum of them.
697 dend_children_heights <- get_childrens_heights(dend_node)
698 attr(dend_node, "height") <- max(dend_children_heights) + diff_height
699
700
701 return(unclass(dend_node))
702 }
703
704 dend <- fix_height_diff_per_branch(dend)
705 class(dend) <- "dendrogram"
706 return(dend)
707 }
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724 #' @title Assign values to nodePar of dendrogram's leaves
725 #' @export
726 #' @description
727 #' Go through the dendrogram leaves and updates the values inside its nodePar
728 #'
729 #' If the value has Inf then the value in edgePar will not be changed.
730 #' @param dend a dendrogram object
731 #' @param value a new value vector for the nodePar attribute. It should be
732 #' the same length as the number of leaves in the tree. If not, it will recycle
733 #' the value and issue a warning.
734 #' @param nodePar the value inside nodePar to adjust.
735 #' @param warn logical (default from dendextend_options("warn") is FALSE).
736 #' Set if warning are to be issued, it is safer to keep this at TRUE,
737 #' but for keeping the noise down, the default is FALSE.
738 #' @param ... not used
739 #' @return
740 #' A dendrogram, after adjusting the nodePar attribute in all of its leaves,
741 #' @seealso \link{get_leaves_attr}
742 #' @examples
743 #'
744 #' \dontrun{
745 #'
746 #' dend <- USArrests[1:5, ] %>%
747 #' dist() %>%
748 #' hclust("ave") %>%
749 #' as.dendrogram()
750 #'
751 #' # reproduces "labels_colors<-"
752 #' # although it does force us to run through the tree twice,
753 #' # hence "labels_colors<-" is better...
754 #' plot(dend)
755 #' dend <- assign_values_to_leaves_nodePar(dend = dend, value = c(3, 2), nodePar = "lab.col")
756 #' plot(dend)
757 #'
758 #' dend <- assign_values_to_leaves_nodePar(dend, 1, "pch")
759 #' plot(dend)
760 #' # fix the annoying pch=1:
761 #' dend <- assign_values_to_leaves_nodePar(dend, NA, "pch")
762 #' plot(dend)
763 #' # adjust the cex:
764 #' dend <- assign_values_to_leaves_nodePar(dend, 19, "pch")
765 #' dend <- assign_values_to_leaves_nodePar(dend, 2, "lab.cex")
766 #' plot(dend)
767 #'
768 #' str(unclass(dend))
769 #'
770 #' get_leaves_attr(dend, "nodePar", simplify = FALSE)
771 #' }
772 #'
773 assign_values_to_leaves_nodePar <- function(dend, value, nodePar, warn = dendextend_options("warn"), ...) {
774 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
775
776 if (missing(value)) {
777 if (warn) warning("value is missing, returning the dendrogram as is.")
778 return(dend)
779 }
780
781 leaves_length <- nleaves(dend) # length(labels(dend)) # it will be faster to use order.dendrogram than labels...
782 if (leaves_length > length(value)) {
783 if (warn) warning("Length of value vector was shorter than the number of leaves - vector value recycled")
784 value <- rep(value, length.out = leaves_length)
785 }
786
787 set_value_to_leaf <- function(dend_node) {
788 if (is.leaf(dend_node)) {
789 i_leaf_number <<- i_leaf_number + 1
790
791 to_update_attr <- !is.infinite2(value[i_leaf_number])
792 if (to_update_attr) {
793 # if(!is.infinite2(value[i_leaf_number])) {
794 attr(dend_node, "nodePar")[nodePar] <- list(value[i_leaf_number]) # this way it doesn't erase other nodePar values (if they exist)
795 }
796
797
798 if (length(attr(dend_node, "nodePar")) == 0) {
799 attr(dend_node, "nodePar") <- NULL # remove nodePar if it is empty
800 } else {
801 # if we have some nodePar, and we don't have pch - let's make
802 # sure it is NA - so that we don't see that annoying dot.
803 if (!("pch" %in% names(attr(dend_node, "nodePar")))) {
804 attr(dend_node, "nodePar")["pch"] <- list(NA)
805 }
806 }
807 }
808 return(unclass(dend_node))
809 }
810 i_leaf_number <- 0
811 new_dend <- dendrapply(dend, set_value_to_leaf)
812 class(new_dend) <- "dendrogram"
813 return(new_dend)
814 }
815
816
817 #
818 # tanglegram(dend1 , dend2, lab.cex = 2, edge.lwd = 3,margin_inner= 5, type = "t", center = TRUE)
819 # attr(dend[[1]], "nodePar")[["pch"]]
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835 #' @title Assign values to edgePar of dendrogram's leaves
836 #' @export
837 #' @description
838 #' Go through the dendrogram leaves and updates the values inside its edgePar
839 #'
840 #' If the value has Inf then the value in edgePar will not be changed.
841 #' @param dend a dendrogram object
842 #' @param value a new value vector for the edgePar attribute. It should be
843 #' the same length as the number of leaves in the tree. If not, it will recycle
844 #' the value and issue a warning.
845 #' @param edgePar the value inside edgePar to adjust.
846 #' @param warn logical (default from dendextend_options("warn") is FALSE).
847 #' Set if warning are to be issued, it is safer to keep this at TRUE,
848 #' but for keeping the noise down, the default is FALSE.
849 #' @param ... not used
850 #' @return
851 #' A dendrogram, after adjusting the edgePar attribute in all of its leaves,
852 #' @seealso \link{get_leaves_attr}, \link{assign_values_to_leaves_nodePar}
853 #' @examples
854 #'
855 #' \dontrun{
856 #'
857 #' dend <- USArrests[1:5, ] %>%
858 #' dist() %>%
859 #' hclust("ave") %>%
860 #' as.dendrogram()
861 #'
862 #' plot(dend)
863 #' dend <- assign_values_to_leaves_edgePar(dend = dend, value = c(3, 2), edgePar = "col")
864 #' plot(dend)
865 #' dend <- assign_values_to_leaves_edgePar(dend = dend, value = c(3, 2), edgePar = "lwd")
866 #' plot(dend)
867 #' dend <- assign_values_to_leaves_edgePar(dend = dend, value = c(3, 2), edgePar = "lty")
868 #' plot(dend)
869 #'
870 #' get_leaves_attr(dend, "edgePar", simplify = FALSE)
871 #' }
872 #'
873 assign_values_to_leaves_edgePar <- function(dend, value, edgePar, warn = dendextend_options("warn"), ...) {
874 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
875
876 if (missing(value)) {
877 if (warn) warning("value is missing, returning the dendrogram as is.")
878 return(dend)
879 }
880
881 leaves_length <- nleaves(dend) # length(labels(dend)) # it will be faster to use order.dendrogram than labels...
882 if (leaves_length > length(value)) {
883 if (warn) warning("Length of value vector was shorter than the number of leaves - vector value recycled")
884 value <- rep(value, length.out = leaves_length)
885 }
886
887 set_value_to_leaf <- function(dend_node) {
888 if (is.leaf(dend_node)) {
889 i_leaf_number <<- i_leaf_number + 1
890
891 to_update_attr <- !is.infinite2(value[i_leaf_number])
892 if (to_update_attr) {
893 # if(!is.infinite2(value[i_leaf_number])) {
894 attr(dend_node, "edgePar")[edgePar] <- list(value[i_leaf_number]) # this way it doesn't erase other edgePar values (if they exist)
895 }
896
897
898 if (length(attr(dend_node, "edgePar")) == 0) {
899 attr(dend_node, "edgePar") <- NULL # remove edgePar if it is empty
900 } else {
901 # if we have some edgePar, and we don't have pch - let's make
902 # sure it is NA - so that we don't see that annoying dot.
903 if (!("pch" %in% names(attr(dend_node, "edgePar")))) {
904 attr(dend_node, "edgePar")["pch"] <- list(NA)
905 }
906 }
907 }
908 return(unclass(dend_node))
909 }
910 i_leaf_number <- 0
911 new_dend <- dendrapply(dend, set_value_to_leaf)
912 class(new_dend) <- "dendrogram"
913 return(new_dend)
914 }
915
916
917
918
919
920
921
922
923 #' @title Assign values to nodePar of dendrogram's nodes
924 #' @export
925 #' @description
926 #' Go through the dendrogram nodes and updates the values inside its nodePar
927 #'
928 #' If the value has Inf then the value in edgePar will not be changed.
929 #'
930 #' @param dend a dendrogram object
931 #' @param value a new value vector for the nodePar attribute. It should be
932 #' the same length as the number of nodes in the tree. If not, it will recycle
933 #' the value and issue a warning.
934 #' @param nodePar the value inside nodePar to adjust.
935 #' This may contain components named pch, cex, col, xpd, and/or bg.
936 #' @param warn logical (default from dendextend_options("warn") is FALSE).
937 #' Set if warning are to be issued, it is safer to keep this at TRUE,
938 #' but for keeping the noise down, the default is FALSE.
939 #' @param ... not used
940 #' @return
941 #' A dendrogram, after adjusting the nodePar attribute in all of its nodes,
942 #' @seealso \link{get_leaves_attr}, \link{assign_values_to_leaves_nodePar}
943 #' @examples
944 #'
945 #' \dontrun{
946 #'
947 #' dend <- USArrests[1:5, ] %>%
948 #' dist() %>%
949 #' hclust("ave") %>%
950 #' as.dendrogram()
951 #'
952 #' # reproduces "labels_colors<-"
953 #' # although it does force us to run through the tree twice,
954 #' # hence "labels_colors<-" is better...
955 #' plot(dend)
956 #' dend2 <- dend %>%
957 #' assign_values_to_nodes_nodePar(value = 19, nodePar = "pch") %>%
958 #' assign_values_to_nodes_nodePar(value = c(1, 2), nodePar = "cex") %>%
959 #' assign_values_to_nodes_nodePar(value = c(2, 1), nodePar = "col")
960 #' plot(dend2)
961 #'
962 #'
963 #' ### Making sure this works for NA with character.
964 #' dend %>%
965 #' assign_values_to_nodes_nodePar(value = 19, nodePar = "pch") %>%
966 #' assign_values_to_nodes_nodePar(value = c("red", NA), nodePar = "col") -> dend2
967 #' plot(dend2)
968 #' }
969 #'
970 assign_values_to_nodes_nodePar <- function(dend, value, nodePar = c("pch", "cex", "col", "xpd", "bg"), warn = dendextend_options("warn"), ...) {
971 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
972
973 if (missing(value)) {
974 warning("value is missing, returning the dendrogram as is.")
975 return(dend)
976 }
977
978 nodePar <- match.arg(nodePar)
979
980
981 nodes_length <- nnodes(dend) # length(labels(dend)) # it will be faster to use order.dendrogram than labels...
982 if (nodes_length > length(value)) {
983 if (warn) warning("Length of value vector was shorter than the number of nodes - vector value recycled")
984 value <- rep(value, length.out = nodes_length)
985 }
986
987 set_value_to_node <- function(dend_node) {
988 i_node_number <<- i_node_number + 1
989
990 to_update_attr <- !is.infinite2(value[i_node_number]) | is.na(value[i_node_number])
991 if (to_update_attr) {
992 # if(!is.infinite2(value[i_node_number])) {
993 attr(dend_node, "nodePar")[nodePar] <- list(value[i_node_number]) # this way it doesn't erase other nodePar values (if they exist)
994 }
995
996 if (length(attr(dend_node, "nodePar")) == 0) {
997 attr(dend_node, "nodePar") <- NULL # remove nodePar if it is empty
998 } else {
999 # if we have some nodePar, and we don't have pch - let's make
1000 # sure it is NA - so that we don't see that annoying dot.
1001 if (!("pch" %in% names(attr(dend_node, "nodePar")))) {
1002 attr(dend_node, "nodePar")["pch"] <- list(NA)
1003 }
1004 }
1005 return(unclass(dend_node))
1006 }
1007 i_node_number <- 0
1008 new_dend <- dendrapply(dend, set_value_to_node)
1009 class(new_dend) <- "dendrogram"
1010 return(new_dend)
1011 }
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 #' @title Assign values to edgePar of dendrogram's branches
1030 #' @export
1031 #' @description
1032 #' Go through the dendrogram branches and updates the values inside its edgePar
1033 #'
1034 #' If the value has Inf then the value in edgePar will not be changed.
1035 #'
1036 #' @param dend a dendrogram object
1037 #' @param value a new value scalar for the edgePar attribute.
1038 #' @param edgePar a character indicating the value inside edgePar to adjust.
1039 #' Can be either "col", "lty", or "lwd".
1040 #' @param skip_leaves logical (FALSE) - should the leaves be skipped/ignored?
1041 #' @param warn logical (default from dendextend_options("warn") is FALSE).
1042 #' Set if warning are to be issued, it is safer to keep this at TRUE,
1043 #' but for keeping the noise down, the default is FALSE.
1044 #' @param ... not used
1045 #' @return
1046 #' A dendrogram, after adjusting the edgePar attribute in all of its branches,
1047 #' @seealso \link{get_root_branches_attr}
1048 #' @examples
1049 #'
1050 #' # This failed before - now it works fine. (thanks to Martin Maechler)
1051 #' dend <- 1:2 %>%
1052 #' dist() %>%
1053 #' hclust() %>%
1054 #' as.dendrogram()
1055 #' dend %>%
1056 #' set("branches_lty", 1:2) %>%
1057 #' set("branches_col", c("topbranch_never_plots", "black", "orange")) %>%
1058 #' plot()
1059 #' \dontrun{
1060 #'
1061 #' dend <- USArrests[1:5, ] %>%
1062 #' dist() %>%
1063 #' hclust() %>%
1064 #' as.dendrogram()
1065 #' plot(dend)
1066 #' dend <- assign_values_to_branches_edgePar(dend = dend, value = 2, edgePar = "lwd")
1067 #' plot(dend)
1068 #' dend <- assign_values_to_branches_edgePar(dend = dend, value = 2, edgePar = "col")
1069 #' plot(dend)
1070 #' dend <- assign_values_to_branches_edgePar(dend = dend, value = "orange", edgePar = "col")
1071 #' plot(dend)
1072 #' dend2 <- assign_values_to_branches_edgePar(dend = dend, value = 2, edgePar = "lty")
1073 #' plot(dend2)
1074 #'
1075 #' dend2 %>%
1076 #' unclass() %>%
1077 #' str()
1078 #' }
1079 #'
1080 assign_values_to_branches_edgePar <- function(dend, value, edgePar, skip_leaves = FALSE, warn = dendextend_options("warn"), ...) {
1081 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
1082
1083 if (missing(value)) {
1084 warning("value is missing, returning the dendrogram as is.")
1085 return(dend)
1086 }
1087
1088 # if we are skipping leaves, than the number of branches should not include the terminal nodes/leaves!
1089 n_branches <- nnodes(dend) - ifelse(skip_leaves, nleaves(dend), 0) # length(labels(dend)) # it will be faster to use order.dendrogram than labels...
1090 if (n_branches > length(value)) {
1091 if (warn) warning("Length of value vector was shorter than the number of leaves - vector value recycled")
1092 value <- rep(value, length.out = n_branches)
1093 }
1094
1095 set_value_to_branch <- function(dend_node) {
1096 # if we ignore leaves, jump the function for leaves:
1097 if (skip_leaves & is.leaf(dend_node)) {
1098 return(unclass(dend_node))
1099 }
1100 # else - keep as usual.
1101
1102 i_node <<- i_node + 1
1103 to_update_attr <- !is.infinite2(value[i_node])
1104 if (to_update_attr) {
1105 attr(dend_node, "edgePar")[edgePar] <- list(value[i_node]) # [i_leaf_number] # this way it doesn't erase other edgePar values (if they exist)
1106 }
1107 if (length(attr(dend_node, "edgePar")) == 0) attr(dend_node, "edgePar") <- NULL # remove edgePar if it is empty
1108 return(unclass(dend_node))
1109 }
1110
1111 i_node <- 0
1112 new_dend <- dendrapply(dend, set_value_to_branch)
1113 class(new_dend) <- "dendrogram"
1114 return(new_dend)
1115 }
1116
1117
1118
1119
1120
1121
1122 #' @title Remove all edgePar values from a dendrogram's branches
1123 #' @export
1124 #' @description
1125 #' Go through the dendrogram branches and remove its edgePar.
1126 #' @param dend a dendrogram object
1127 #' @param ... not used
1128 #' @return
1129 #' A dendrogram, after removing the edgePar attribute in all of its branches,
1130 #' @seealso \link{get_root_branches_attr}, \link{assign_values_to_branches_edgePar}
1131 #' @examples
1132 #'
1133 #' \dontrun{
1134 #'
1135 #' dend <- USArrests[1:5, ] %>%
1136 #' dist() %>%
1137 #' hclust() %>%
1138 #' as.dendrogram()
1139 #' dend <- color_branches(dend, 3)
1140 #' par(mfrow = c(1, 2))
1141 #' plot(dend)
1142 #' plot(remove_branches_edgePar(dend))
1143 #' }
1144 #'
1145 remove_branches_edgePar <- function(dend, ...) {
1146 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
1147
1148 remove_edgePar_from_branch <- function(dend_node) {
1149 attr(dend_node, "edgePar") <- NULL # remove edgePar
1150 return(unclass(dend_node))
1151 }
1152 new_dend <- dendrapply(dend, remove_edgePar_from_branch)
1153 class(new_dend) <- "dendrogram"
1154 return(new_dend)
1155 }
1156
1157
1158
1159
1160 #' @title Remove all nodePar values from a dendrogram's nodes
1161 #' @export
1162 #' @description
1163 #' Go through the dendrogram nodes and remove its nodePar
1164 #' @param dend a dendrogram object
1165 #' @param ... not used
1166 #' @return
1167 #' A dendrogram, after removing the nodePar attribute in all of its nodes,
1168 #' @seealso \link{get_root_branches_attr}, \link{assign_values_to_branches_edgePar}
1169 #' @examples
1170 #'
1171 #' \dontrun{
1172 #'
1173 #' dend <- USArrests[1:5, ] %>%
1174 #' dist() %>%
1175 #' hclust() %>%
1176 #' as.dendrogram()
1177 #' dend <- color_branches(dend, 3)
1178 #' par(mfrow = c(1, 2))
1179 #' plot(dend)
1180 #' plot(remove_branches_edgePar(dend))
1181 #' }
1182 #'
1183 remove_nodes_nodePar <- function(dend, ...) {
1184 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
1185
1186 remove_nodePar_from_node <- function(dend_node) {
1187 attr(dend_node, "nodePar") <- NULL # remove nodePar
1188 return(unclass(dend_node))
1189 }
1190 new_dend <- dendrapply(dend, remove_nodePar_from_node)
1191 class(new_dend) <- "dendrogram"
1192 return(new_dend)
1193 }
1194
1195
1196
1197
1198 #' @title Remove all nodePar values from a dendrogram's leaves
1199 #' @export
1200 #' @description
1201 #' Go through the dendrogram leaves and remove its nodePar.
1202 #' @param dend a dendrogram object
1203 #' @param ... not used
1204 #' @return
1205 #' A dendrogram, after removing the nodePar attribute in all of its leaves,
1206 #' @seealso \link{get_leaves_attr}, \link{assign_values_to_leaves_nodePar}
1207 #' @examples
1208 #'
1209 #' \dontrun{
1210 #'
1211 #' dend <- USArrests[1:5, ] %>%
1212 #' dist() %>%
1213 #' hclust() %>%
1214 #' as.dendrogram()
1215 #'
1216 #' dend <- color_labels(dend, 3)
1217 #' par(mfrow = c(1, 2))
1218 #' plot(dend)
1219 #' plot(remove_leaves_nodePar(dend))
1220 #'
1221 #'
1222 #' get_leaves_attr(dend, "nodePar")
1223 #' get_leaves_attr(remove_leaves_nodePar(dend), "nodePar")
1224 #' }
1225 #'
1226 remove_leaves_nodePar <- function(dend, ...) {
1227 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
1228
1229 remove_nodePar_from_leaf <- function(dend_node) {
1230 if (is.leaf(dend_node)) {
1231 attr(dend_node, "nodePar") <- NULL # remove nodePar if it is empty
1232 }
1233 return(unclass(dend_node))
1234 }
1235 new_dend <- dendrapply(dend, remove_nodePar_from_leaf)
1236 class(new_dend) <- "dendrogram"
1237 return(new_dend)
1238 }
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251 # nleaves(dend)
1252 # nleaves(dend[[1]])
1253 # nleaves(dend[[2]])
1254
1255
1256 #' @title Fix members attr in a dendrogram
1257 #' @export
1258 #' @description
1259 #' Fix members attr in a dendrogram after (for example), the tree was pruned
1260 #' or manipulated.
1261 #' @param dend a dendrogram object
1262 #' @param ... not used
1263 #' @return
1264 #' A dendrogram, after adjusting the members attr in all of its nodes.
1265 #' @examples
1266 #'
1267 #' # define dendrogram object to play with:
1268 #' hc <- hclust(dist(USArrests[1:3, ]), "ave")
1269 #' dend <- as.dendrogram(hc)
1270 #' # plot(dend)
1271 #' # prune one leaf
1272 #' dend[[2]] <- dend[[2]][[1]]
1273 #' # plot(dend)
1274 #' dend # but it is NO LONGER true that it has 3 members total!
1275 #' fix_members_attr.dendrogram(dend) # it now knows it has only 2 members :)
1276 #'
1277 #' hc <- hclust(dist(USArrests[1:3, ]), "ave")
1278 #' dend <- as.dendrogram(hc)
1279 #'
1280 #' identical(prune_leaf(dend, "Alaska"), fix_members_attr.dendrogram(prune_leaf(dend, "Alaska")))
1281 #' str(unclass(prune_leaf(dend, "Alaska")))
1282 #' str(unclass(fix_members_attr.dendrogram(prune_leaf(dend, "Alaska"))))
1283 fix_members_attr.dendrogram <- function(dend, ...) {
1284 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
1285
1286
1287 fix_members_attr_per_node <- function(dend_node) {
1288 if (!is.leaf(dend_node)) attr(dend_node, "members") <- nleaves(dend_node, method = "order")
1289 return(unclass(dend_node))
1290 }
1291 # mtrace(".change.label.by.mat")
1292 dend <- dendrapply(dend, fix_members_attr_per_node)
1293 class(dend) <- "dendrogram"
1294 return(dend)
1295 }
1296 #
1297
1298
1299
1300 #' @title Fix rank of leaves order values in a dendrogram
1301 #' @export
1302 #' @description
1303 #' Generally, leaves order value should be a sequence of integer values.
1304 #' From 1 to nleaves(dend).
1305 #' This function fixes trees by using \link{rank} on existing leaves order
1306 #' values.
1307 #' @param dend a dendrogram object
1308 #' @param ... not used
1309 #' @return
1310 #' A dendrogram, after fixing its leaves order values.
1311 #' @seealso
1312 #' \link{prune}
1313 #' @examples
1314 #'
1315 #' # define dendrogram object to play with:
1316 #' dend <- USArrests[1:4, ] %>%
1317 #' dist() %>%
1318 #' hclust(method = "ave") %>%
1319 #' as.dendrogram()
1320 #' # plot(dend)
1321 #' order.dendrogram(dend)
1322 #' dend2 <- prune(dend, "Alaska")
1323 #' order.dendrogram(dend2)
1324 #' order.dendrogram(rank_order.dendrogram(dend2))
1325 rank_order.dendrogram <- function(dend, ...) {
1326 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
1327
1328 order.dendrogram(dend) <- rank(order.dendrogram(dend), ties.method = "first")
1329
1330 return(dend)
1331 }
1332 #