Debian Codegraph @

R/highlight_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 normalize <- function(x) {
21 # https://stat.ethz.ch/pipermail/r-help//2012-October/336676.html
22 # https://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range
23 # https://en.wikipedia.org/wiki/Feature_scaling
24 # https://en.wikipedia.org/wiki/Normalization_(statistics)
25 (x - min(x, na.rm = TRUE)) /
26 (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
27 }
28
29 map_values_to_scale <- function(values, scale) {
30 location <- round(normalize(values) * (length(scale) - 1)) + 1
31 scale[location]
32 }
33
34
35 #' @export
36 #' @rdname highlight_branches
37 highlight_branches_col <- function(dend, values = rev(viridis(1000, end = .9)), ...) {
38 if (missing(values)) values <- rev(viridis(1000, end = .9))
39
40 # library(viridis)
41 # h <- get_branches_heights(dend, sort = FALSE, include_leaves = T)
42 # h <- get_nodes_attr(dend, "height", include_leaves = TRUE, na.rm = TRUE) # works
43 h <- get_branches_heights(dend, sort = FALSE, include_leaves = TRUE) # works
44 # h <- round(normalize(h) * (length(cols)-1) ) + 1
45 # library(scales)
46 # f <- scales::col_numeric(rev(cols), h)
47 # c(f(h))
48 # cols <- cols[h]
49 cols <- map_values_to_scale(h, values)
50 dend2 <- set(dend, "branches_col", cols)
51 # plot(dend2)
52
53 dend2
54 }
55
56
57 #' @export
58 #' @rdname highlight_branches
59 highlight_branches_lwd <- function(dend, values = seq(1, 10, length.out = 1000), ...) {
60 if (missing(values)) values <- seq(1, 10, length.out = 1000)
61
62 # library(viridis)
63 # h <- get_branches_heights(dend, sort = FALSE, include_leaves = T)
64 # h <- get_nodes_attr(dend, "height", include_leaves = TRUE, na.rm = TRUE) # works
65 h <- get_branches_heights(dend, sort = FALSE, include_leaves = TRUE) # works
66
67 lwds <- map_values_to_scale(h, values)
68 # dend2 <- set(dend, "branches_col", cols)
69
70 dend2 <- set(dend, "branches_lwd", lwds)
71 # plot(dend2)
72
73 dend2
74 }
75
76
77
78
79
80
81 #' @title Highlight a dendrogram's branches heights via color and line-width
82 #' @rdname highlight_branches
83 #' @export
84 #'
85 #' @description
86 #' Highlights (update) the color (col) and/or line width (lwd) of each branch in
87 #' a dendrogram based on it's node's height.
88 #' This is a powerful pre-processing for a \link{tanglegram} plot of two dendrograms, as
89 #' it emphasizes the toplogical structure of each tree (and hence, their similarity and differences).
90 #'
91 #' The colors are based on the viridis pallette, and the line width is on the range of 1 to 10.
92 #' These can be manually changed when using highlight_branches_col and highlight_branches_lwd
93 #' respectively.
94 #'
95 #' @param dend a \link{dendrogram} tree (to be turned into a ggdend object)
96 #' @param type a character vector. Either "col", "lwd", or both. Based on
97 #' whichever is chosen the dendrogram's branches will be updated.
98 #' @param values the gradient of values to be used for each branch.
99 #' The colors are based on the viridis pallette, and the line width is on the range of 1 to 10.
100 #' These can be manually changed when using highlight_branches_col and highlight_branches_lwd
101 #' respectively.
102 #' @param ... Currently ignored.
103 #'
104 #' @seealso
105 #'
106 #' \link{set}, \link{color_branches}, \link{get_branches_heights},
107 #' \link[viridis]{viridis}
108 #'
109 #' @return
110 #' A modified \link{dendrogram}, with colors/line-width in the branches
111 #' that are proportional to each branche's height (measured by its lower tip).
112 #'
113 #' @examples
114 #'
115 #'
116 #' dat <- iris[1:20, -5]
117 #' hca <- hclust(dist(dat))
118 #' hca2 <- hclust(dist(dat), method = "single")
119 #' dend <- as.dendrogram(hca)
120 #' dend2 <- as.dendrogram(hca2)
121 #'
122 #' par(mfrow = c(1, 3))
123 #' dend %>%
124 #' highlight_branches_col() %>%
125 #' plot(main = "Coloring branches")
126 #' dend %>%
127 #' highlight_branches_lwd() %>%
128 #' plot(main = "Emphasizing line-width")
129 #' dend %>%
130 #' highlight_branches() %>%
131 #' plot(main = "Emphasizing color\n and line-width")
132 #'
133 #' library(viridis)
134 #' par(mfrow = c(1, 3))
135 #' dend %>%
136 #' highlight_branches_col() %>%
137 #' plot(main = "Coloring branches \n(default is reversed viridis)")
138 #' dend %>%
139 #' highlight_branches_col(viridis(100)) %>%
140 #' plot(main = "It is better to use\nlighter colors in the leaves")
141 #' dend %>%
142 #' highlight_branches_col(rev(magma(1000))) %>%
143 #' plot(main = "The magma color pallatte\n is also good")
144 #'
145 #' dl <- dendlist(dend, dend2)
146 #' tanglegram(dl,
147 #' sort = TRUE, common_subtrees_color_lines = FALSE,
148 #' highlight_distinct_edges = FALSE, highlight_branches_lwd = FALSE
149 #' )
150 #' tanglegram(dl)
151 #' tanglegram(dl, fast = TRUE)
152 #'
153 #' dl <- dendlist(highlight_branches(dend), highlight_branches(dend2))
154 #' tanglegram(dl, sort = TRUE, common_subtrees_color_lines = FALSE, highlight_distinct_edges = FALSE)
155 #'
156 #' dend %>%
157 #' set("highlight_branches_col") %>%
158 #' plot()
159 #'
160 #' dl <- dendlist(dend, dend2) %>% set("highlight_branches_col")
161 #' tanglegram(dl, sort = TRUE, common_subtrees_color_lines = FALSE, highlight_distinct_edges = FALSE)
162 #'
163 #'
164 #' # This is also useful for heatmaps
165 #' # --------------------------
166 #' # library(dendextend)
167 #'
168 #' x <- as.matrix(datasets::mtcars)
169 #'
170 #' Rowv <- x %>%
171 #' dist() %>%
172 #' hclust() %>%
173 #' as.dendrogram() %>%
174 #' set("branches_k_color", k = 3) %>%
175 #' set("highlight_branches_lwd") %>%
176 #' ladderize()
177 #' # rotate_DendSer(ser_weight = dist(x))
178 #' Colv <- x %>%
179 #' t() %>%
180 #' dist() %>%
181 #' hclust() %>%
182 #' as.dendrogram() %>%
183 #' set("branches_k_color", k = 2) %>%
184 #' set("highlight_branches_lwd") %>%
185 #' ladderize()
186 #' # rotate_DendSer(ser_weight = dist(t(x)))
187 #'
188 #' library(gplots)
189 #' heatmap.2(x, Rowv = Rowv, Colv = Colv)
190 highlight_branches <- function(dend, type = c("col", "lwd"), ...) {
191 if (!is.dendrogram(dend)) stop("dend is not a dendrogram (and it needs to be...)")
192 # type <- match.arg(type)
193
194 if ("col" %in% type) dend <- highlight_branches_col(dend)
195 if ("lwd" %in% type) dend <- highlight_branches_lwd(dend)
196
197 dend
198 }