Debian Codegraph @

R/set.dendrogram.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 #' @title Set (/update) features to a dendrogram
22 #' @export
23 #' @rdname set
24 #'
25 #' @description
26 #' a master function for updating various attributes and
27 #' features of dendrogram objects.
28 #'
29 #' @param dend a tree (\link{dendrogram}, or \link{dendlist})
30 #' @param what a character indicating what is the property of
31 #' the tree that should be set/updated. (see the usage and the example section
32 #' for the different options)
33 #' @param value an object with the value to set in the dendrogram tree.
34 #' (the type of the value depends on the "what")
35 #' @param order_value logical. Default is FALSE. If TRUE, it means the order of
36 #' the value is in the order of the data which produced the \link{hclust}
37 #' or \link{dendrogram} - and will reorder the value to conform with the order
38 #' of the labels in the dendrogram.
39 #' @param ... passed to the specific function for more options.
40 #' @param which an integer vector indicating, in the case "dend" is
41 #' a dendlist, on which of the trees should the modification be performed.
42 #' If missing - the change will be performed on all of dends in the dendlist.
43 #'
44 #' @details
45 #' This is a wrapper function for many of the main tasks we
46 #' might wish to perform on a dendrogram before plotting.
47 #'
48 #' The options of by_labels_branches_col, by_labels_branches_lwd, by_labels_branches_lty
49 #' have extra parameters: `type` and `TF_value` (`attr` is autofilled).
50 #' For by_lists_branches_col, by_lists_branches_lwd,
51 #' by_lists_branches_lty, they get extra parameter: TF_value (`attr` is autofilled).
52 #' You can read more about them here:
53 #' \link{branches_attr_by_labels} and \link{branches_attr_by_lists}
54 #'
55 #' The "what" parameter" can accept the following options:
56 #'
57 #' \itemize{
58 #' \item{labels - set the labels (\link{labels<-.dendrogram})}
59 #' \item{labels_colors - set the labels' colors (\link{color_labels})}
60 #' \item{labels_cex - set the labels' size (\link{assign_values_to_leaves_nodePar})}
61 #' \item{labels_to_character - set the labels' to be characters}
62 #' \item{leaves_pch - set the leaves' point type (\link{assign_values_to_leaves_nodePar}). A leave is the terminal node of the tree.}
63 #' \item{leaves_cex - set the leaves' point size (\link{assign_values_to_leaves_nodePar}). For using this you MUST also set leaves_pch, a good value to use is 19.}
64 #' \item{leaves_col - set the leaves' point color (\link{assign_values_to_leaves_nodePar}). For using this you MUST also set leaves_pch, a good value to use is 19.}
65 #' \item{leaves_bg - set the leaves' point fill color (\link{assign_values_to_leaves_nodePar}). For using this you MUST also set leaves_pch with values from 21-25.}
66 #' \item{nodes_pch - set the nodes' point type (\link{assign_values_to_nodes_nodePar})}
67 #' \item{nodes_cex - set the nodes' point size (\link{assign_values_to_nodes_nodePar})}
68 #' \item{nodes_col - set the nodes' point color (\link{assign_values_to_nodes_nodePar})}
69 #' \item{nodes_bg - set the nodes' point fill color (\link{assign_values_to_nodes_nodePar}). For using this you MUST also set leaves_pch with values from 21-25.}
70 #' \item{hang_leaves - hang the leaves (\link{hang.dendrogram})}
71 #' \item{branches_k_color - color the branches (\link{color_branches}), a \code{k} parameter needs to be supplied.}
72 #' \item{branches_k_lty - updates the lwd of the branches (similar to branches_k_color), a \code{k} parameter needs to be supplied.}
73 #' \item{branches_col - set the color of branches (\link{assign_values_to_branches_edgePar}) }
74 #' \item{branches_lwd - set the line width of branches (\link{assign_values_to_branches_edgePar}) }
75 #' \item{branches_lty - set the line type of branches (\link{assign_values_to_branches_edgePar}) }
76 #' \item{by_labels_branches_col - set the color of branches with specific labels (\link{branches_attr_by_labels}) }
77 #' \item{by_labels_branches_lwd - set the line width of branches with specific labels (\link{branches_attr_by_labels}) }
78 #' \item{by_labels_branches_lty - set the line type of branches with specific labels (\link{branches_attr_by_labels}) }
79 #' \item{by_lists_branches_col - set the color of branches from the root of the tree down to (possibly inner) nodes with specified members (\link{branches_attr_by_lists}) }
80 #' \item{by_lists_branches_lwd - set the line width of branches from the root of the tree down to (possibly inner) nodes with specified members (\link{branches_attr_by_lists}) }
81 #' \item{by_lists_branches_lty - set the line type of branches from the root of the tree down to (possibly inner) nodes with specified members (\link{branches_attr_by_lists}) }
82 #' \item{highlight_branches_col - highlight branches color based on branches' heights (\link{highlight_branches_col}) }
83 #' \item{highlight_branches_lwd - highlight branches line-width based on branches' heights (\link{highlight_branches_lwd}) }
84 #' \item{clear_branches - clear branches' attributes (\link{remove_branches_edgePar})}
85 #' \item{clear_leaves - clear leaves' attributes (\link{remove_branches_edgePar})}
86 #' }
87 #'
88 #'
89 #' @seealso
90 #'
91 #' \link{labels<-.dendrogram}, \link{labels_colors<-},
92 #' \link{hang.dendrogram}, \link{color_branches},
93 #' \link{assign_values_to_leaves_nodePar},
94 #' \link{assign_values_to_branches_edgePar},
95 #' \link{remove_branches_edgePar}, \link{remove_leaves_nodePar},
96 #' \link{noded_with_condition}, \link{branches_attr_by_labels}, \link{branches_attr_by_lists},
97 #' \link{dendrogram}
98 #'
99 #' @return
100 #' An updated dendrogram (or dendlist), with some change to
101 #' the parameters of it
102 #'
103 #' @examples
104 #'
105 #' \dontrun{
106 #'
107 #' set.seed(23235)
108 #' ss <- sample(1:150, 10)
109 #'
110 #' # Getting the dend object
111 #' dend <- iris[ss, -5] %>%
112 #' dist() %>%
113 #' hclust() %>%
114 #' as.dendrogram()
115 #' dend %>% plot()
116 #'
117 #' dend %>% labels()
118 #' dend %>%
119 #' set("labels", 1:10) %>%
120 #' labels()
121 #' dend %>%
122 #' set("labels", 1:10) %>%
123 #' plot()
124 #' dend %>%
125 #' set("labels_color") %>%
126 #' plot()
127 #' dend %>%
128 #' set("labels_col", c(1, 2)) %>%
129 #' plot() # Works also with partial matching :)
130 #' dend %>%
131 #' set("labels_cex", c(1, 1.2)) %>%
132 #' plot()
133 #' dend %>%
134 #' set("leaves_pch", NA) %>%
135 #' plot()
136 #' dend %>%
137 #' set("leaves_pch", c(1:5)) %>%
138 #' plot()
139 #' dend %>%
140 #' set("leaves_pch", c(19, 19, NA)) %>%
141 #' set("leaves_cex", c(1, 2)) %>%
142 #' plot()
143 #' dend %>%
144 #' set("leaves_pch", c(19, 19, NA)) %>%
145 #' set("leaves_cex", c(1, 2)) %>%
146 #' set("leaves_col", c(1, 1, 2, 2)) %>%
147 #' plot()
148 #' dend %>%
149 #' set("hang") %>%
150 #' plot()
151 #'
152 #' # using bg for leaves and nodes
153 #'
154 #' set.seed(23235)
155 #' ss <- sample(1:150, 25)
156 #'
157 #' # Getting the dend object
158 #' dend25 <- iris[ss, -5] %>%
159 #' dist() %>%
160 #' hclust() %>%
161 #' as.dendrogram()
162 #'
163 #' dend25 %>%
164 #' set("labels", 1:25) %>%
165 #' set("nodes_pch", 21) %>% # set all nodes to be pch 21
166 #' set("nodes_col", "darkred") %>%
167 #' set("nodes_bg", "gold") %>%
168 #' set("leaves_pch", 1:25) %>% # Change the leaves pch to move from 1 to 25
169 #' set("leaves_col", "darkred") %>%
170 #' set("leaves_bg", "gold") %>%
171 #' plot(main = "pch 21 to 25 supports the\nnodes_bg and leaves_bg parameters")
172 #'
173 #'
174 #' dend %>%
175 #' set("branches_k_col") %>%
176 #' plot()
177 #' dend %>%
178 #' set("branches_k_col", c(1, 2)) %>%
179 #' plot()
180 #' dend %>%
181 #' set("branches_k_col", c(1, 2, 3), k = 3) %>%
182 #' plot()
183 #' dend %>%
184 #' set("branches_k_col", k = 3) %>%
185 #' plot()
186 #'
187 #' dend %>%
188 #' set("branches_k_lty", k = 3) %>%
189 #' plot()
190 #' dend %>%
191 #' set("branches_k_col", k = 3) %>%
192 #' set("branches_k_lty", k = 3) %>%
193 #' plot()
194 #'
195 #' dend %>%
196 #' set("branches_col", c(1, 2, 1, 2, NA)) %>%
197 #' plot()
198 #' dend %>%
199 #' set("branches_lwd", c(2, 1, 2)) %>%
200 #' plot()
201 #' dend %>%
202 #' set("branches_lty", c(1, 2, 1)) %>%
203 #' plot()
204 #'
205 #' # clears all of the things added to the leaves
206 #' dend %>%
207 #' set("labels_color", c(19, 19, NA)) %>%
208 #' set("leaves_pch", c(19, 19, NA)) %>% # plot
209 #' set("clear_leaves") %>% # remove all of what was done until this point
210 #' plot()
211 #' # Different order
212 #' dend %>%
213 #' set("leaves_pch", c(19, 19, NA)) %>%
214 #' set("labels_color", c(19, 19, NA)) %>%
215 #' set("clear_leaves") %>%
216 #' plot()
217 #'
218 #'
219 #' # doing this without chaining (%>%) will NOT be fun:
220 #' dend %>%
221 #' set("labels", 1:10) %>%
222 #' set("labels_color") %>%
223 #' set("branches_col", c(1, 2, 1, 2, NA)) %>%
224 #' set("branches_lwd", c(2, 1, 2)) %>%
225 #' set("branches_lty", c(1, 2, 1)) %>%
226 #' set("hang") %>%
227 #' plot()
228 #'
229 #' par(mfrow = c(1, 3))
230 #' dend %>%
231 #' set("highlight_branches_col") %>%
232 #' plot()
233 #' dend %>%
234 #' set("highlight_branches_lwd") %>%
235 #' plot()
236 #' dend %>%
237 #' set("highlight_branches_col") %>%
238 #' set("highlight_branches_lwd") %>%
239 #' plot()
240 #' par(mfrow = c(1, 1))
241 #'
242 #' #----------------------------
243 #' # Examples for: by_labels_branches_col, by_labels_branches_lwd, by_labels_branches_lty
244 #'
245 #' old_labels <- labels(dend)
246 #' dend %>%
247 #' set("labels", seq_len(nleaves(dend))) %>%
248 #' set("by_labels_branches_col", value = c(1:4, 7), TF_values = 'gold') %>%
249 #' set("by_labels_branches_lwd", value = c(1:4, 7), TF_values = 5) %>%
250 #' set("by_labels_branches_lty", value = c(1:4, 7), TF_values = 2) %>%
251 #' set("labels", old_labels) %>%
252 #' plot()
253 #'
254 #' dend %>%
255 #' set("labels", seq_len(nleaves(dend))) %>%
256 #' set("by_labels_branches_col", c(1:4, 7), type = "any", TF_values = c(4, 2)) %>%
257 #' set("by_labels_branches_lwd", c(1:4, 7), type = "all", TF_values = c(4, 1)) %>%
258 #' set("by_labels_branches_lty", c(1:4, 7), TF_values = c(4, 1)) %>%
259 #' plot()
260 #'
261 #' #---- using order_value
262 #' # This is probably not what you want, since cutree
263 #' # returns clusters in the order of the original data:
264 #' dend %>%
265 #' set("labels_colors", cutree(dend, k = 3)) %>%
266 #' plot()
267 #' # The way to fix it, is to use order_value = TRUE
268 #' # so that value is assumed to be in the order of the data:
269 #' dend %>%
270 #' set("labels_colors", cutree(dend, k = 3), order_value = TRUE) %>%
271 #' plot()
272 #'
273 #'
274 #' #----------------------------
275 #' # Example for: by_lists_branches_col, by_lists_branches_lwd, by_lists_branches_lty
276 #'
277 #' L <- list(c("109", "123", "126", "145"), "29", c("59", "67", "97"))
278 #' dend %>%
279 #' set("by_lists_branches_col", L, TF_value = "blue") %>%
280 #' set("by_lists_branches_lwd", L, TF_value = 4) %>%
281 #' set("by_lists_branches_lty", L, TF_value = 3) %>%
282 #' plot()
283 #'
284 #'
285 #' #----------------------------
286 #' # A few dendlist examples:
287 #' dendlist(dend, dend) %>%
288 #' set("hang") %>%
289 #' plot()
290 #' dendlist(dend, dend) %>%
291 #' set("branches_k_col", k = 3) %>%
292 #' plot()
293 #' dendlist(dend, dend) %>%
294 #' set("labels_col", c(1, 2)) %>%
295 #' plot()
296 #'
297 #' dendlist(dend, dend) %>%
298 #' set("hang") %>%
299 #' set("labels_col", c(1, 2), which = 1) %>%
300 #' set("branches_k_col", k = 3, which = 2) %>%
301 #' set("labels_cex", 1.2) %>%
302 #' plot()
303 #'
304 #'
305 #' #----------------------------
306 #' # example of modifying the dendrogram in a heatmap:
307 #'
308 #' library(gplots)
309 #' data(mtcars)
310 #' x <- as.matrix(mtcars)
311 #' rc <- rainbow(nrow(x), start = 0, end = .3)
312 #' cc <- rainbow(ncol(x), start = 0, end = .3)
313 #'
314 #' ##
315 #' ##' demonstrate the effect of row and column dendrogram options
316 #' ##
317 #' Rowv_dend <- x %>%
318 #' dist() %>%
319 #' hclust() %>%
320 #' as.dendrogram() %>%
321 #' set("branches_k", k = 3) %>%
322 #' set("branches_lwd", 2) %>%
323 #' ladderize() # rotate_DendSer
324 #' Colv_dend <- t(x) %>%
325 #' dist() %>%
326 #' hclust() %>%
327 #' as.dendrogram() %>%
328 #' set("branches_k", k = 3) %>%
329 #' set("branches_lwd", 2) %>%
330 #' ladderize() # rotate_DendSer
331 #' heatmap.2(x, Rowv = Rowv_dend, Colv = Colv_dend)
332 #' }
333 set <- function(dend, ...) {
334 UseMethod("set")
335 }
336
337
338 #' @export
339 #' @rdname set
340 set.dendrogram <-
341 function(dend,
342 what = c(
343 "labels",
344 "labels_colors",
345 "labels_cex",
346 "labels_to_character",
347 "leaves_pch",
348 "leaves_cex",
349 "leaves_col",
350 "leaves_bg",
351 "nodes_pch",
352 "nodes_cex",
353 "nodes_col",
354 "nodes_bg",
355 "hang_leaves",
356 "rank_branches",
357 "branches_k_color",
358 "branches_k_lty",
359 "branches_col",
360 "branches_lwd",
361 "branches_lty",
362 "by_labels_branches_col",
363 "by_labels_branches_lwd",
364 "by_labels_branches_lty",
365 "by_lists_branches_col",
366 "by_lists_branches_lwd",
367 "by_lists_branches_lty",
368 "highlight_branches_col",
369 "highlight_branches_lwd",
370 "clear_branches",
371 "clear_leaves"
372 ),
373 value,
374 order_value = FALSE,
375 ...) {
376 if (missing(what)) {
377 if (dendextend_options("warn")) warning("'what' is missing, returning the dendrogram as is")
378 return(dend)
379 }
380
381 if (order_value) value <- value[order.dendrogram(dend)]
382
383 what <- match.arg(what)
384 dend <- switch(what,
385 # labels = dendextend:::`labels<-.dendrogram`(dend, value = value)
386 labels = `labels<-.dendrogram`(dend, value = value, ...),
387 labels_colors = color_labels(dend, col = value, ...),
388 # labels_colors = `labels_colors<-`(dend, value = value, ...),
389 # labels_colors = assign_values_to_leaves_nodePar(dend, value, "lab.col", ...),
390 labels_cex = assign_values_to_leaves_nodePar(dend, value, "lab.cex", ...),
391 labels_to_character = set(dend, what = "labels", value = as.character(labels(dend)), ...),
392 leaves_pch = assign_values_to_leaves_nodePar(dend, value, "pch", ...),
393 leaves_cex = assign_values_to_leaves_nodePar(dend, value, "cex", ...),
394 leaves_col = assign_values_to_leaves_nodePar(dend, value, "col", ...),
395 leaves_bg = assign_values_to_leaves_nodePar(dend, value, "bg", ...),
396 nodes_pch = assign_values_to_nodes_nodePar(dend, value, "pch", ...),
397 nodes_cex = assign_values_to_nodes_nodePar(dend, value, "cex", ...),
398 nodes_col = assign_values_to_nodes_nodePar(dend, value, "col", ...),
399 nodes_bg = assign_values_to_nodes_nodePar(dend, value, "bg", ...),
400 hang_leaves = hang.dendrogram(dend, hang = ifelse(missing(value), .1, value), ...),
401 rank_branches = rank_branches(dend, ...),
402 branches_k_color = color_branches(dend, col = value, ...),
403 branches_k_lty = lty_branches(dend, lty = value, ...),
404 branches_col = assign_values_to_branches_edgePar(dend, value = value, edgePar = "col", ...),
405 branches_lwd = assign_values_to_branches_edgePar(dend, value = value, edgePar = "lwd", ...),
406 branches_lty = assign_values_to_branches_edgePar(dend, value = value, edgePar = "lty", ...),
407 by_labels_branches_col = branches_attr_by_labels(dend, labels = value, attr = "col", ...),
408 by_labels_branches_lwd = branches_attr_by_labels(dend, labels = value, attr = "lwd", ...),
409 by_labels_branches_lty = branches_attr_by_labels(dend, labels = value, attr = "lty", ...),
410 by_lists_branches_col = branches_attr_by_lists(dend, lists = value, attr = "col", ...),
411 by_lists_branches_lwd = branches_attr_by_lists(dend, lists = value, attr = "lwd", ...),
412 by_lists_branches_lty = branches_attr_by_lists(dend, lists = value, attr = "lty", ...),
413 highlight_branches_col = highlight_branches_col(dend, values = value, ...),
414 highlight_branches_lwd = highlight_branches_lwd(dend, values = value, ...),
415 clear_branches = remove_branches_edgePar(dend, ...),
416 clear_leaves = remove_leaves_nodePar(dend, ...)
417 )
418 dend
419 }
420
421
422
423 #' @export
424 #' @rdname set
425 set.dendlist <- function(dend, ..., which) {
426 if (missing(which)) which <- 1:length(dend)
427
428 for (i in which) {
429 dend[[i]] <- set(dend[[i]], ...)
430 }
431 dend
432 }
433
434
435 #' @export
436 #' @rdname set
437 set.data.table <- function(...) {
438 warning("set function has been overridden from data.table - which means that data.table's set() is now slower. You may solve this by using the prefix 'data.table::'' in your for loop.")
439 # library(data.table)
440 data.table::set(...)
441 }