Debian Codegraph @

man/color_branches.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/color_branches.R
3 \name{color_branches}
4 \alias{color_branches}
5 \alias{colour_branches}
6 \alias{branches_color}
7 \title{Color tree's branches according to sub-clusters}
8 \source{
9 This function is a derived work from the \code{\link[dendroextras]{color_clusters}}
10 function, with some ideas from the \code{\link[dendroextras]{slice}} function -
11 both are from the {\pkg{dendroextras}} package by jefferis.
12
13 It extends it by using \link[dendextend]{cutree.dendrogram} - allowing
14 the function to work for trees that hclust can not handle
15 (unbranched and non-ultrametric trees).
16 Also, it allows REPEATED cluster color assignments to branches on to
17 the same tree. Something which the original function was not able to handle.
18 }
19 \usage{
20 color_branches(
21 dend,
22 k = NULL,
23 h = NULL,
24 col,
25 groupLabels = NULL,
26 clusters,
27 warn = dendextend_options("warn"),
28 ...
29 )
30 }
31 \arguments{
32 \item{dend}{A \code{dendrogram} or \code{hclust} tree object}
33
34 \item{k}{number of groups (passed to \code{\link[dendextend]{cutree}})}
35
36 \item{h}{height at which to cut tree (passed to \code{\link[dendextend]{cutree}})}
37
38 \item{col}{Function or vector of Colors. By default it tries to use
39 \link[colorspace]{rainbow_hcl} from the \code{colorspace} package.
40 (with parameters c=90 and l=50). If \code{colorspace} is not available,
41 It will fall back on the \link{rainbow} function.}
42
43 \item{groupLabels}{If TRUE add numeric group label - see Details for options}
44
45 \item{clusters}{an integer vector of clusters. This is passed to \link{branches_attr_by_clusters}.
46 This HAS to be of the same length as the number of leaves.
47 Items that belong to no cluster should get the value 0.
48 The vector should be of the same order as that of the labels in the dendrogram.
49 If you create the clusters from something like \link{cutree} you would first
50 need to use \link{order.dendrogram} on it, before using it in the function.}
51
52 \item{warn}{logical (default from dendextend_options("warn") is FALSE).
53 Set if warning are to be issued, it is safer to keep this at TRUE,
54 but for keeping the noise down, the default is FALSE.}
55
56 \item{...}{ignored.}
57 }
58 \value{
59 a tree object of class \link{dendrogram}.
60 }
61 \description{
62 This function is for dendrogram and hclust objects.
63 This function colors both the terminal leaves of a dend's cluster and the edges
64 leading to those leaves. The edgePar attribute of nodes will be augmented by
65 a new list item col.
66 The groups will be defined by a call to \code{\link[dendextend]{cutree}}
67 using the k or h parameters.
68
69 If col is a color vector with a different length than the number of clusters
70 (k) - then a recycled color vector will be used.
71 }
72 \details{
73 If \code{groupLabels=TRUE} then numeric group labels will be added
74 to each cluster. If a vector is supplied then these entries will be used as
75 the group labels. If a function is supplied then it will be passed a
76 numeric vector of groups (e.g. 1:5) and must return the formatted group
77 labels.
78
79 If the \link{labels} of the dendrogram are NOT character (but, for example
80 integers) - they are coerced into character. This step is essential for the
81 proper operation of the function. A dendrogram labels might happen to be
82 integers if they are based on an \link{hclust} performed on a \link{dist}
83 of an object without \link{rownames}.
84 }
85 \examples{
86
87 \dontrun{
88 par(mfrow = c(1, 2))
89 dend <- USArrests \%>\%
90 dist() \%>\%
91 hclust(method = "ave") \%>\%
92 as.dendrogram()
93 d1 <- color_branches(dend, k = 5, col = c(3, 1, 1, 4, 1))
94 plot(d1) # selective coloring of branches :)
95 d2 <- color_branches(dend, 5)
96 plot(d2)
97
98 par(mfrow = c(1, 2))
99 d1 <- color_branches(dend, 5, col = c(3, 1, 1, 4, 1), groupLabels = TRUE)
100 plot(d1) # selective coloring of branches :)
101 d2 <- color_branches(dend, 5, groupLabels = TRUE)
102 plot(d2)
103
104 par(mfrow = c(1, 3))
105 d5 <- color_branches(dend, 5)
106 plot(d5)
107 d5g <- color_branches(dend, 5, groupLabels = TRUE)
108 plot(d5g)
109 d5gr <- color_branches(dend, 5, groupLabels = as.roman)
110 plot(d5gr)
111
112 par(mfrow = c(1, 1))
113
114 # messy - but interesting:
115 dend_override <- color_branches(dend, 2, groupLabels = as.roman)
116 dend_override <- color_branches(dend_override, 4, groupLabels = as.roman)
117 dend_override <- color_branches(dend_override, 7, groupLabels = as.roman)
118 plot(dend_override)
119
120 d5 <- color_branches(dend = dend[[1]], k = 5)
121
122
123 library(dendextend)
124 data(iris, envir = environment())
125 d_iris <- dist(iris[, -5])
126 hc_iris <- hclust(d_iris)
127 dend_iris <- as.dendrogram(hc_iris)
128 dend_iris <- color_branches(dend_iris, k = 3)
129
130 library(colorspace)
131 labels_colors(dend_iris) <-
132 rainbow_hcl(3)[sort_levels_values(
133 as.numeric(iris[, 5])[order.dendrogram(dend_iris)]
134 )]
135
136 plot(dend_iris,
137 main = "Clustered Iris dataset",
138 sub = "labels are colored based on the true cluster"
139 )
140
141
142
143 # cutree(dend_iris,k=3, order_clusters_as_data=FALSE,
144 # try_cutree_hclust=FALSE)
145 # cutree(dend_iris,k=3, order_clusters_as_data=FALSE)
146
147 library(colorspace)
148
149 data(iris, envir = environment())
150 d_iris <- dist(iris[, -5])
151 hc_iris <- hclust(d_iris)
152 labels(hc_iris) # no labels, because "iris" has no row names
153 dend_iris <- as.dendrogram(hc_iris)
154 is.integer(labels(dend_iris)) # this could cause problems...
155
156 iris_species <- rev(levels(iris[, 5]))
157 dend_iris <- color_branches(dend_iris, k = 3, groupLabels = iris_species)
158 is.character(labels(dend_iris)) # labels are no longer "integer"
159
160 # have the labels match the real classification of the flowers:
161 labels_colors(dend_iris) <-
162 rainbow_hcl(3)[sort_levels_values(
163 as.numeric(iris[, 5])[order.dendrogram(dend_iris)]
164 )]
165
166 # We'll add the flower type
167 labels(dend_iris) <- paste(as.character(iris[, 5])[order.dendrogram(dend_iris)],
168 "(", labels(dend_iris), ")",
169 sep = ""
170 )
171
172 dend_iris <- hang.dendrogram(dend_iris, hang_height = 0.1)
173
174 # reduce the size of the labels:
175 dend_iris <- assign_values_to_leaves_nodePar(dend_iris, 0.5, "lab.cex")
176
177 par(mar = c(3, 3, 3, 7))
178 plot(dend_iris,
179 main = "Clustered Iris dataset
180 (the labels give the true flower species)",
181 horiz = TRUE, nodePar = list(cex = .007)
182 )
183 legend("topleft", legend = iris_species, fill = rainbow_hcl(3))
184 a <- dend_iris[[1]]
185 dend_iris1 <- color_branches(a, k = 3)
186 plot(dend_iris1)
187
188 # str(dendrapply(d2, unclass))
189 # unclass(d1)
190
191 c(1:5) \%>\% # take some data
192 dist() \%>\% # calculate a distance matrix,
193 # on it compute hierarchical clustering using the "average" method,
194 hclust(method = "single") \%>\%
195 as.dendrogram() \%>\%
196 color_branches(k = 3) \%>\%
197 plot() # nice, returns the tree as is...
198
199
200 # Example of the "clusters" parameter
201 par(mfrow = c(1, 2))
202 dend <- c(1:5) \%>\%
203 dist() \%>\%
204 hclust() \%>\%
205 as.dendrogram()
206 dend \%>\%
207 color_branches(k = 3) \%>\%
208 plot()
209 dend \%>\%
210 color_branches(clusters = c(1, 1, 2, 2, 3)) \%>\%
211 plot()
212
213
214 # another example, based on the question here:
215 # https://stackoverflow.com/q/45432271/256662
216
217
218 library(cluster)
219 set.seed(999)
220 iris2 <- iris[sample(x = 1:150, size = 50, replace = F), ]
221 clust <- diana(iris2)
222 dend <- as.dendrogram(clust)
223
224 temp_col <- c("red", "blue", "green")[as.numeric(iris2$Species)]
225 temp_col <- temp_col[order.dendrogram(dend)]
226 temp_col <- factor(temp_col, unique(temp_col))
227
228 library(dendextend)
229 dend \%>\%
230 color_branches(clusters = as.numeric(temp_col), col = levels(temp_col)) \%>\%
231 set("labels_colors", as.character(temp_col)) \%>\%
232 plot()
233 }
234
235 }
236 \seealso{
237 \code{\link[dendextend]{cutree}},\code{\link{dendrogram}},
238 \code{\link{hclust}}, \code{\link{labels_colors}},
239 \code{\link{branches_attr_by_clusters}}, \link{get_leaves_branches_col},
240 \link{color_labels}
241 }
242 \author{
243 Tal Galili, extensively based on code by Gregory Jefferis
244 }