Debian Codegraph @

man/get_subdendrograms.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/get_subdendrograms.R
3 \name{get_subdendrograms}
4 \alias{get_subdendrograms}
5 \title{Extract a list of \emph{k} subdendrograms from a given dendrogram
6 object}
7 \usage{
8 get_subdendrograms(dend, k, order_clusters_as_data = FALSE, ...)
9 }
10 \arguments{
11 \item{dend}{a dendrogram object}
12
13 \item{k}{the number of subdendrograms that should be extracted}
14
15 \item{order_clusters_as_data}{passed to \link[dendextend]{cutree}, default is FALSE
16 (while the cutree default is TRUE). The reason is since it's easier to look at the dendrogram plot
17 and then get subtrees that are in the same order is in the plot/dendrogram object.
18 This is in contrast to more traditional use of cutree, where it is used with the original order or rows from the data.}
19
20 \item{...}{parameters that should be passed to the cutree
21 \code{\link{cutree.dendrogram}}}
22 }
23 \value{
24 A list of \emph{k} subdendrograms, based on the cutree
25 \code{\link{cutree.dendrogram}} clustering
26 clusters.
27 }
28 \description{
29 Extracts a list (\link{dendlist}) of subdendrogram structures based on the cutree \code{\link{cutree.dendrogram}} function
30 from a given dendrogram object. It can be useful in case we're interested in a visual investigation of
31 specific clustering results.
32 }
33 \examples{
34
35 # needed packages:
36 # install.packages(gplots)
37 # install.packages(viridis)
38 # install.packages(devtools)
39 # devtools::install_github('talgalili/dendextend') #' dendextend from github
40
41 # define dendrogram object to play with:
42 dend <- iris[1:20, -5] \%>\%
43 dist() \%>\%
44 hclust() \%>\%
45 as.dendrogram() \%>\%
46 # set("labels_to_character") \%>\%
47 color_branches(k = 5)
48 labels(dend) <- letters[1:20]
49 plot(dend)
50 dend_list <- get_subdendrograms(dend, 5)
51 lapply(dend_list, labels)
52 # [[1]]
53 # [1] "a" "b"
54 #
55 # [[2]]
56 # [1] "c" "d" "e" "f" "g"
57 #
58 # [[3]]
59 # [1] "h" "i"
60 #
61 # [[4]]
62 # [1] "j" "k" "l" "m"
63 #
64 # [[5]]
65 # [1] "n" "o" "p" "q" "r" "s" "t"
66
67 # define dendrogram object to play with:
68 dend <- iris[, -5] \%>\%
69 dist() \%>\%
70 hclust() \%>\%
71 as.dendrogram() \%>\%
72 set("labels_to_character") \%>\%
73 color_branches(k = 5)
74 dend_list <- get_subdendrograms(dend, 5)
75
76 # Plotting the result
77 par(mfrow = c(2, 3))
78 plot(dend, main = "Original dendrogram")
79 sapply(dend_list, plot)
80
81 # plot a heatmap of only one of the sub dendrograms
82 par(mfrow = c(1, 1))
83 library(gplots)
84 sub_dend <- dend_list[[1]] #' get the sub dendrogram
85 # make sure of the size of the dend
86 nleaves(sub_dend)
87 length(order.dendrogram(sub_dend))
88 # get the subset of the data
89 subset_iris <- as.matrix(iris[order.dendrogram(sub_dend), -5])
90 # update the dendrogram's internal order so to not cause an error in heatmap.2
91 order.dendrogram(sub_dend) <- as.integer(rank(order.dendrogram(sub_dend)))
92 heatmap.2(subset_iris, Rowv = sub_dend, trace = "none", col = viridis::viridis(100))
93 }