Debian Codegraph @

R/labels_colors.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 #' @title Retrieve/assign colors to the labels of a dendrogram
24 #' @export
25 #' @rdname labels_colors
26 #' @description Retrieve/assign colors to the labels of a dendrogram. Note that usually dend objects come without any color assignment (and the output will be NULL, until colors are assigned).
27 #' @param dend a dendrogram object
28 #' @param labels Boolean (default is TRUE), should the returned vector of colors
29 #' return with the leaves labels as names.
30 #' @param ... not used
31 #' @param value a vector of colors to be used as new label's colors for the dendrogram
32 #' @source Heavily inspired by the code in the example of \link{dendrapply},
33 #' so credit should go to Martin Maechler.
34 #' I also implemented some ideas from Gregory Jefferis's dendroextras package
35 #' (having the "names" of the returned vector be the labels).
36 #' @return
37 #' A vector with the dendrogram's labels colors (or a colored dendrogram,
38 #' in case assignment is used). The colors are labeled.
39 #' @seealso \code{\link[dendextend]{cutree}},\code{\link{dendrogram}},
40 #' \code{\link{hclust}}, \code{\link{color_labels}}, \code{\link{color_branches}},
41 #' \link{assign_values_to_leaves_edgePar}, \link{get_leaves_branches_col}
42 #' @examples
43 #' # define dendrogram object to play with:
44 #' hc <- hclust(dist(USArrests[1:3, ]), "ave")
45 #' dend <- as.dendrogram(hc)
46 #'
47 #' # Defaults:
48 #' labels_colors(dend)
49 #' plot(dend)
50 #'
51 #' # let's add some color:
52 #' labels_colors(dend) <- 2:4
53 #' labels_colors(dend)
54 #' plot(dend)
55 #'
56 #'
57 #' # doesn't work...
58 #' # get_nodes_attr(dend, "nodePar", include_branches = FALSE)
59 #'
60 #' # changing color to black
61 #' labels_colors(dend) <- 1
62 #' labels_colors(dend)
63 #' plot(dend)
64 #'
65 #' # removing color (and the nodePar completely - if it has no other attributed but lab.col)
66 #' suppressWarnings(labels_colors(dend) <- NULL)
67 #' labels_colors(dend)
68 #' plot(dend)
69 labels_colors <- function(dend, labels = TRUE, ...) {
70 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
71
72 col <- NULL
73
74 get_col_from_leaf <- function(dend_node) {
75 if (is.leaf(dend_node)) {
76 i_leaf_number <<- i_leaf_number + 1
77 col[i_leaf_number] <<- attr(dend_node, "nodePar")[["lab.col"]]
78 if (!is.null(col) & labels) names(col)[i_leaf_number] <<- attr(dend_node, "label")
79 }
80 return(dend_node)
81 }
82 # mtrace(".change.label.by.mat")
83 i_leaf_number <- 0
84 # here I don't care about the classes of the branches of the dend.
85 dendrapply(dend, get_col_from_leaf)
86 return(col)
87 }
88
89
90 #' @export
91 #' @rdname labels_colors
92 labels_col <- labels_colors
93
94
95
96 #' @export
97 #' @rdname labels_colors
98 "labels_colors<-" <- function(dend, ..., value) {
99 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
100
101 if (missing(value)) {
102 if (dendextend_options("warn")) warning("Color values are missing, using default (different) colors")
103 tree_size <- nleaves(dend)
104 value <- rainbow_fun(tree_size)
105 }
106
107
108 col <- value
109 leaves_length <- length(order.dendrogram(dend)) # length(labels(dend)) # it will be faster to use order.dendrogram than labels...
110 if (leaves_length > length(col)) {
111 if (dendextend_options("warn")) warning("Length of color vector was shorter than the number of leaves - vector color recycled")
112 col <- rep(col, length.out = leaves_length)
113 }
114
115 set.col.to.leaf <- function(dend_node) {
116 if (is.leaf(dend_node)) {
117 i_leaf_number <<- i_leaf_number + 1
118 if (is.null(attr(dend_node, "nodePar"))) {
119 attr(dend_node, "nodePar") <-
120 list(
121 lab.col = col[i_leaf_number],
122 pch = NA
123 )
124 } else {
125 # attr(dend_node, "nodePar") <- within(attr(dend_node, "nodePar"), {lab.col <- col[i_leaf_number]}) # this way it doesn't erase other nodePar values (if they exist)
126 # Using as.list in order to make the code more robust
127 # attr(dend_node, "nodePar") <- within(as.list(attr(dend_node, "nodePar")), {lab.col <- col[i_leaf_number]}) # this way it doesn't erase other nodePar values (if they exist)
128 attr(dend_node, "nodePar")[["lab.col"]] <- col[i_leaf_number] # this way it doesn't erase other nodePar values (if they exist)
129 }
130
131 if (length(attr(dend_node, "nodePar")) == 0) attr(dend_node, "nodePar") <- NULL # remove nodePar if it is empty
132 }
133 return(unclass(dend_node))
134 }
135 i_leaf_number <- 0
136 new_dend <- dendrapply(dend, set.col.to.leaf)
137 class(new_dend) <- "dendrogram"
138 return(new_dend)
139 }
140
141
142 `labels_colors<-.dendrogram` <- `labels_colors<-`
143
144
145
146 #
147
148
149 #' @title Retrieve/assign cex to the labels of a dendrogram
150 #' @rdname labels_cex
151 #' @description Retrieve/assign cex to the labels of a dendrogram
152 #' @export
153 #' @param dend a dendrogram object
154 #' @param ... not used
155 #' @param value a vector of cex to be used as new label's size for the dendrogram
156 #' @return
157 #' A vector with the dendrogram's labels sizes (NULL if none are supplied).
158 #' @examples
159 #' # define dendrogram object to play with:
160 #' dend <- as.dendrogram(hclust(dist(USArrests[1:3, ]), "ave"))
161 #'
162 #' # Defaults:
163 #' labels_cex(dend)
164 #' plot(dend)
165 #'
166 #' # let's add some color:
167 #' labels_cex(dend) <- 1:3
168 #' labels_cex(dend)
169 #' plot(dend)
170 #'
171 #' labels_cex(dend) <- 1
172 #' labels_cex(dend)
173 #' plot(dend)
174 labels_cex <- function(dend, ...) {
175 # get_leaves_attr(dend, attribute = "nodePar", simplify = FALSE)
176 unlist(dendrapply(dend, function(node) {
177 attr(node, "nodePar")$`lab.cex`
178 }))
179 }
180
181 #' @export
182 #' @rdname labels_cex
183 "labels_cex<-" <- function(dend, ..., value) {
184 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
185 assign_values_to_leaves_nodePar(dend, value, "lab.cex", ...)
186 }
187
188 `labels_cex<-.dendrogram` <- `labels_cex<-`
189
190
191
192
193
194
195
196
197
198
199
200
201
202 #' Color unique labels in a dendrogram
203 #'
204 #' @param dend a dend object
205 #' @param ... NOT USED
206 #'
207 #' @return
208 #' A dendrogram after the colors of its labels have been updated (a different color for each unique label).
209 #' @export
210 #'
211 #' @examples
212 #'
213 #' x <- c(2011, 2011, 2012, 2012, 2015, 2015, 2015)
214 #' names(x) <- x
215 #' dend <- as.dendrogram(hclust(dist(x)))
216 #'
217 #' par(mfrow = c(1, 2))
218 #' plot(dend)
219 #' dend2 <- color_unique_labels(dend)
220 #' plot(dend2)
221 color_unique_labels <- function(dend, ...) {
222 # if(!require(dendextend)) install.packages("dendextend")
223 # if(!require(colorspace)) install.packages("colorspace")
224 # library("dendextend")
225 # original request: https://stackoverflow.com/questions/33567508/how-to-color-the-same-labels-on-dendorgram-in-one-colour-in-r/
226
227 n_unique_labels <- length(unique(labels(dend)))
228 colors <- colorspace::rainbow_hcl(n_unique_labels)
229 labels_number <- as.numeric(factor(labels(dend)))
230 labels_colors(dend) <- colors[labels_number]
231 dend
232 }
233
234
235
236
237
238
239
240
241 #
242 # #### Tests:
243 # # define dendrogram object to play with:
244 # hc <- hclust(dist(USArrests[1:3,]), "ave")
245 # dend <- as.dendrogram(hc)
246 #
247 # # Defaults:
248 # labels_colors(dend)
249 # plot(dend)
250 #
251 # # let's add some color:
252 # labels_colors(dend) <- 2:4
253 # plot(dend)
254 #
255
256 # dend_node <- structure(3L, members = 1L, height = 0, label = "123", leaf = TRUE, nodePar = structure(19, .Names = "pch"))
257 # as.list(attr(dend_node, "nodePar"))
258 # str(attr(dend_node, "nodePar"))
259 # within
260 # within(as.list(attr(dend_node, "nodePar")), {print(ls())})
261 # within(as.list(attr(dend_node, "nodePar")), {bbb = 4})
262 # within(attr(dend_node, "nodePar"), {lab.col <- col[i_leaf_number]}) # this way it doesn't erase other nodePar values (if they exist)
263 #