| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/branches_attr_by.R |
| 3 |
\name{branches_attr_by_clusters} |
| 4 |
\alias{branches_attr_by_clusters} |
| 5 |
\title{Change col/lwd/lty of branches based on clusters} |
| 6 |
\usage{ |
| 7 |
branches_attr_by_clusters( |
| 8 |
dend, |
| 9 |
clusters, |
| 10 |
values, |
| 11 |
attr = c("col", "lwd", "lty"), |
| 12 |
branches_changed_have_which_labels = c("any", "all"), |
| 13 |
... |
| 14 |
) |
| 15 |
} |
| 16 |
\arguments{ |
| 17 |
\item{dend}{a dendrogram dend} |
| 18 |
|
| 19 |
\item{clusters}{an integer vector of clusters. |
| 20 |
This HAS to be of the same length as the number of leaves. |
| 21 |
Items that belong to no cluster should get the value 0. |
| 22 |
The vector should be of the same order as that of the labels in the dendrogram. |
| 23 |
If you create the clusters from something like \link{cutree} you would first |
| 24 |
need to use \link{order.dendrogram} on it, before using it in the function.} |
| 25 |
|
| 26 |
\item{values}{the attributes to use for non 0 values. |
| 27 |
This should be of the same length as the number of unique non-0 clusters. |
| 28 |
If it is shorter, it is recycled. |
| 29 |
|
| 30 |
OR, this can also be of the same length as the number of leaves in the tree |
| 31 |
In which case, the values will be aggreagted (i.e.: \link{tapply}), to match |
| 32 |
the number of clusters. The first value of each cluster will be used as the main |
| 33 |
value. |
| 34 |
|
| 35 |
TODO: So far, the function doesn't deal well with NA values. (this might be changed in the future)} |
| 36 |
|
| 37 |
\item{attr}{a character with one of the following values: col/lwd/lty} |
| 38 |
|
| 39 |
\item{branches_changed_have_which_labels}{character with either "any" (default) or "all". |
| 40 |
Inidicates how the branches should be updated.} |
| 41 |
|
| 42 |
\item{...}{ignored.} |
| 43 |
} |
| 44 |
\value{ |
| 45 |
A dendrogram with modified branches (col/lwd/lty). |
| 46 |
} |
| 47 |
\description{ |
| 48 |
The user supplies a dend, a vector of clusters, and what to modify (and how). |
| 49 |
|
| 50 |
And the function returns a dendrogram with branches col/lwd/lty accordingly. |
| 51 |
(the function assumes unique labels) |
| 52 |
} |
| 53 |
\details{ |
| 54 |
This is probably NOT a very fast implementation of the function, but it works. |
| 55 |
|
| 56 |
This function was designed to enable the manipulation (mainly coloring) of |
| 57 |
branches, based on the results from the \link[dynamicTreeCut]{cutreeDynamic} |
| 58 |
function. |
| 59 |
} |
| 60 |
\examples{ |
| 61 |
|
| 62 |
\dontrun{ |
| 63 |
|
| 64 |
### Getting the hc object |
| 65 |
iris_dist <- iris[, -5] \%>\% dist() |
| 66 |
hc <- iris_dist \%>\% hclust() |
| 67 |
# This is how it looks without any colors: |
| 68 |
dend <- as.dendrogram(hc) |
| 69 |
plot(dend) |
| 70 |
|
| 71 |
# Both functions give the same outcome |
| 72 |
# options 1: |
| 73 |
dend \%>\% |
| 74 |
set("branches_k_color", k = 4) \%>\% |
| 75 |
plot() |
| 76 |
# options 2: |
| 77 |
clusters <- cutree(dend, 4)[order.dendrogram(dend)] |
| 78 |
dend \%>\% |
| 79 |
branches_attr_by_clusters(clusters) \%>\% |
| 80 |
plot() |
| 81 |
|
| 82 |
# and the second option is much slower: |
| 83 |
system.time(set(dend, "branches_k_color", k = 4)) # 0.26 sec |
| 84 |
system.time(branches_attr_by_clusters(dend, clusters)) # 1.61 sec |
| 85 |
# BUT, it also allows us to do more flaxible things! |
| 86 |
|
| 87 |
#-------------------------- |
| 88 |
# Plotting dynamicTreeCut |
| 89 |
#-------------------------- |
| 90 |
|
| 91 |
# let's get the clusters |
| 92 |
library(dynamicTreeCut) |
| 93 |
clusters <- cutreeDynamic(hc, distM = as.matrix(iris_dist)) |
| 94 |
# we need to sort them to the order of the dendrogram: |
| 95 |
clusters <- clusters[order.dendrogram(dend)] |
| 96 |
|
| 97 |
# get some functions: |
| 98 |
library(colorspace) |
| 99 |
no0_unique <- function(x) { |
| 100 |
u_x <- unique(x) |
| 101 |
u_x[u_x != 0] |
| 102 |
} |
| 103 |
|
| 104 |
clusters_numbers <- no0_unique(clusters) |
| 105 |
n_clusters <- length(clusters_numbers) |
| 106 |
cols <- rainbow_hcl(n_clusters) |
| 107 |
dend2 <- branches_attr_by_clusters(dend, clusters, values = cols) |
| 108 |
# dend2 <- branches_attr_by_clusters(dend, clusters) |
| 109 |
plot(dend2) |
| 110 |
# add colored bars: |
| 111 |
ord_cols <- rainbow_hcl(n_clusters)[order(clusters_numbers)] |
| 112 |
tmp_cols <- rep(1, length(clusters)) |
| 113 |
tmp_cols[clusters != 0] <- ord_cols[clusters != 0][clusters] |
| 114 |
colored_bars(tmp_cols, y_shift = -1.1, rowLabels = "") |
| 115 |
# all of the ordering is to handle the fact that the cluster numbers are not ascending... |
| 116 |
|
| 117 |
# How is this compared with the usual cutree? |
| 118 |
dend3 <- color_branches(dend, k = n_clusters) |
| 119 |
labels(dend2) <- as.character(labels(dend2)) |
| 120 |
# this needs fixing, since the labels are not character! |
| 121 |
# Well, both cluster solutions are not perfect, but at least they are interesting... |
| 122 |
tanglegram(dend2, dend3, |
| 123 |
main_left = "cutreeDynamic", main_right = "cutree", |
| 124 |
columns_width = c(5, .5, 5), |
| 125 |
color_lines = cols[iris[order.dendrogram(dend2), 5]] |
| 126 |
) |
| 127 |
# (Notice how the color_lines is of the true Species of each Iris) |
| 128 |
# The main difference is at the bottom, |
| 129 |
} |
| 130 |
} |
| 131 |
\seealso{ |
| 132 |
\link{branches_attr_by_labels}, |
| 133 |
\link{get_leaves_attr}, \link{nnodes}, \link{nleaves} |
| 134 |
\link[dynamicTreeCut]{cutreeDynamic}, |
| 135 |
\link[WGCNA]{plotDendroAndColors} |
| 136 |
} |