File: branches_attr_by_clusters.Rd

package info (click to toggle)
r-cran-dendextend 1.16.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,936 kB
  • sloc: sh: 13; makefile: 2
file content (136 lines) | stat: -rw-r--r-- 4,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/branches_attr_by.R
\name{branches_attr_by_clusters}
\alias{branches_attr_by_clusters}
\title{Change col/lwd/lty of branches based on clusters}
\usage{
branches_attr_by_clusters(
  dend,
  clusters,
  values,
  attr = c("col", "lwd", "lty"),
  branches_changed_have_which_labels = c("any", "all"),
  ...
)
}
\arguments{
\item{dend}{a dendrogram dend}

\item{clusters}{an integer vector of clusters.
This HAS to be of the same length as the number of leaves.
Items that belong to no cluster should get the value 0.
The vector should be of the same order as that of the labels in the dendrogram.
If you create the clusters from something like \link{cutree} you would first
need to use \link{order.dendrogram} on it, before using it in the function.}

\item{values}{the attributes to use for non 0 values.
This should be of the same length as the number of unique non-0 clusters.
If it is shorter, it is recycled.

OR, this can also be of the same length as the number of leaves in the tree
In which case, the values will be aggreagted (i.e.: \link{tapply}), to match
the number of clusters. The first value of each cluster will be used as the main
value.

TODO: So far, the function doesn't deal well with NA values. (this might be changed in the future)}

\item{attr}{a character with one of the following values: col/lwd/lty}

\item{branches_changed_have_which_labels}{character with either "any" (default) or "all".
Inidicates how the branches should be updated.}

\item{...}{ignored.}
}
\value{
A dendrogram with modified branches (col/lwd/lty).
}
\description{
The user supplies a dend, a vector of clusters, and what to modify (and how).

And the function returns a dendrogram with branches col/lwd/lty accordingly.
(the function assumes unique labels)
}
\details{
This is probably NOT a very fast implementation of the function, but it works.

This function was designed to enable the manipulation (mainly coloring) of
branches, based on the results from the \link[dynamicTreeCut]{cutreeDynamic}
function.
}
\examples{

\dontrun{

### Getting the hc object
iris_dist <- iris[, -5] \%>\% dist()
hc <- iris_dist \%>\% hclust()
# This is how it looks without any colors:
dend <- as.dendrogram(hc)
plot(dend)

# Both functions give the same outcome
# options 1:
dend \%>\%
  set("branches_k_color", k = 4) \%>\%
  plot()
# options 2:
clusters <- cutree(dend, 4)[order.dendrogram(dend)]
dend \%>\%
  branches_attr_by_clusters(clusters) \%>\%
  plot()

# and the second option is much slower:
system.time(set(dend, "branches_k_color", k = 4)) # 0.26 sec
system.time(branches_attr_by_clusters(dend, clusters)) # 1.61 sec
# BUT, it also allows us to do more flaxible things!

#--------------------------
#   Plotting dynamicTreeCut
#--------------------------

# let's get the clusters
library(dynamicTreeCut)
clusters <- cutreeDynamic(hc, distM = as.matrix(iris_dist))
# we need to sort them to the order of the dendrogram:
clusters <- clusters[order.dendrogram(dend)]

# get some functions:
library(colorspace)
no0_unique <- function(x) {
  u_x <- unique(x)
  u_x[u_x != 0]
}

clusters_numbers <- no0_unique(clusters)
n_clusters <- length(clusters_numbers)
cols <- rainbow_hcl(n_clusters)
dend2 <- branches_attr_by_clusters(dend, clusters, values = cols)
# dend2 <- branches_attr_by_clusters(dend, clusters)
plot(dend2)
# add colored bars:
ord_cols <- rainbow_hcl(n_clusters)[order(clusters_numbers)]
tmp_cols <- rep(1, length(clusters))
tmp_cols[clusters != 0] <- ord_cols[clusters != 0][clusters]
colored_bars(tmp_cols, y_shift = -1.1, rowLabels = "")
# all of the ordering is to handle the fact that the cluster numbers are not ascending...

# How is this compared with the usual cutree?
dend3 <- color_branches(dend, k = n_clusters)
labels(dend2) <- as.character(labels(dend2))
# this needs fixing, since the labels are not character!
# Well, both cluster solutions are not perfect, but at least they are interesting...
tanglegram(dend2, dend3,
  main_left = "cutreeDynamic", main_right = "cutree",
  columns_width = c(5, .5, 5),
  color_lines = cols[iris[order.dendrogram(dend2), 5]]
)
# (Notice how the color_lines is of the true Species of each Iris)
# The main difference is at the bottom,
}
}
\seealso{
\link{branches_attr_by_labels},
\link{get_leaves_attr}, \link{nnodes}, \link{nleaves}
\link[dynamicTreeCut]{cutreeDynamic},
\link[WGCNA]{plotDendroAndColors}
}