| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/set.dendrogram.R |
| 3 |
\name{set} |
| 4 |
\alias{set} |
| 5 |
\alias{set.dendrogram} |
| 6 |
\alias{set.dendlist} |
| 7 |
\alias{set.data.table} |
| 8 |
\title{Set (/update) features to a dendrogram} |
| 9 |
\usage{ |
| 10 |
set(dend, ...) |
| 11 |
|
| 12 |
\method{set}{dendrogram}( |
| 13 |
dend, |
| 14 |
what = c("labels", "labels_colors", "labels_cex", "labels_to_character", "leaves_pch", |
| 15 |
"leaves_cex", "leaves_col", "leaves_bg", "nodes_pch", "nodes_cex", "nodes_col", |
| 16 |
"nodes_bg", "hang_leaves", "rank_branches", "branches_k_color", "branches_k_lty", |
| 17 |
"branches_col", "branches_lwd", "branches_lty", "by_labels_branches_col", |
| 18 |
"by_labels_branches_lwd", "by_labels_branches_lty", "by_lists_branches_col", |
| 19 |
"by_lists_branches_lwd", "by_lists_branches_lty", "highlight_branches_col", |
| 20 |
"highlight_branches_lwd", "clear_branches", |
| 21 |
"clear_leaves"), |
| 22 |
value, |
| 23 |
order_value = FALSE, |
| 24 |
... |
| 25 |
) |
| 26 |
|
| 27 |
\method{set}{dendlist}(dend, ..., which) |
| 28 |
|
| 29 |
\method{set}{data.table}(...) |
| 30 |
} |
| 31 |
\arguments{ |
| 32 |
\item{dend}{a tree (\link{dendrogram}, or \link{dendlist})} |
| 33 |
|
| 34 |
\item{...}{passed to the specific function for more options.} |
| 35 |
|
| 36 |
\item{what}{a character indicating what is the property of |
| 37 |
the tree that should be set/updated. (see the usage and the example section |
| 38 |
for the different options)} |
| 39 |
|
| 40 |
\item{value}{an object with the value to set in the dendrogram tree. |
| 41 |
(the type of the value depends on the "what")} |
| 42 |
|
| 43 |
\item{order_value}{logical. Default is FALSE. If TRUE, it means the order of |
| 44 |
the value is in the order of the data which produced the \link{hclust} |
| 45 |
or \link{dendrogram} - and will reorder the value to conform with the order |
| 46 |
of the labels in the dendrogram.} |
| 47 |
|
| 48 |
\item{which}{an integer vector indicating, in the case "dend" is |
| 49 |
a dendlist, on which of the trees should the modification be performed. |
| 50 |
If missing - the change will be performed on all of dends in the dendlist.} |
| 51 |
} |
| 52 |
\value{ |
| 53 |
An updated dendrogram (or dendlist), with some change to |
| 54 |
the parameters of it |
| 55 |
} |
| 56 |
\description{ |
| 57 |
a master function for updating various attributes and |
| 58 |
features of dendrogram objects. |
| 59 |
} |
| 60 |
\details{ |
| 61 |
This is a wrapper function for many of the main tasks we |
| 62 |
might wish to perform on a dendrogram before plotting. |
| 63 |
|
| 64 |
The options of by_labels_branches_col, by_labels_branches_lwd, by_labels_branches_lty |
| 65 |
have extra parameters: `type` and `TF_value` (`attr` is autofilled). |
| 66 |
For by_lists_branches_col, by_lists_branches_lwd, |
| 67 |
by_lists_branches_lty, they get extra parameter: TF_value (`attr` is autofilled). |
| 68 |
You can read more about them here: |
| 69 |
\link{branches_attr_by_labels} and \link{branches_attr_by_lists} |
| 70 |
|
| 71 |
The "what" parameter" can accept the following options: |
| 72 |
|
| 73 |
\itemize{ |
| 74 |
\item{labels - set the labels (\link{labels<-.dendrogram})} |
| 75 |
\item{labels_colors - set the labels' colors (\link{color_labels})} |
| 76 |
\item{labels_cex - set the labels' size (\link{assign_values_to_leaves_nodePar})} |
| 77 |
\item{labels_to_character - set the labels' to be characters} |
| 78 |
\item{leaves_pch - set the leaves' point type (\link{assign_values_to_leaves_nodePar}). A leave is the terminal node of the tree.} |
| 79 |
\item{leaves_cex - set the leaves' point size (\link{assign_values_to_leaves_nodePar}). For using this you MUST also set leaves_pch, a good value to use is 19.} |
| 80 |
\item{leaves_col - set the leaves' point color (\link{assign_values_to_leaves_nodePar}). For using this you MUST also set leaves_pch, a good value to use is 19.} |
| 81 |
\item{leaves_bg - set the leaves' point fill color (\link{assign_values_to_leaves_nodePar}). For using this you MUST also set leaves_pch with values from 21-25.} |
| 82 |
\item{nodes_pch - set the nodes' point type (\link{assign_values_to_nodes_nodePar})} |
| 83 |
\item{nodes_cex - set the nodes' point size (\link{assign_values_to_nodes_nodePar})} |
| 84 |
\item{nodes_col - set the nodes' point color (\link{assign_values_to_nodes_nodePar})} |
| 85 |
\item{nodes_bg - set the nodes' point fill color (\link{assign_values_to_nodes_nodePar}). For using this you MUST also set leaves_pch with values from 21-25.} |
| 86 |
\item{hang_leaves - hang the leaves (\link{hang.dendrogram})} |
| 87 |
\item{branches_k_color - color the branches (\link{color_branches}), a \code{k} parameter needs to be supplied.} |
| 88 |
\item{branches_k_lty - updates the lwd of the branches (similar to branches_k_color), a \code{k} parameter needs to be supplied.} |
| 89 |
\item{branches_col - set the color of branches (\link{assign_values_to_branches_edgePar}) } |
| 90 |
\item{branches_lwd - set the line width of branches (\link{assign_values_to_branches_edgePar}) } |
| 91 |
\item{branches_lty - set the line type of branches (\link{assign_values_to_branches_edgePar}) } |
| 92 |
\item{by_labels_branches_col - set the color of branches with specific labels (\link{branches_attr_by_labels}) } |
| 93 |
\item{by_labels_branches_lwd - set the line width of branches with specific labels (\link{branches_attr_by_labels}) } |
| 94 |
\item{by_labels_branches_lty - set the line type of branches with specific labels (\link{branches_attr_by_labels}) } |
| 95 |
\item{by_lists_branches_col - set the color of branches from the root of the tree down to (possibly inner) nodes with specified members (\link{branches_attr_by_lists}) } |
| 96 |
\item{by_lists_branches_lwd - set the line width of branches from the root of the tree down to (possibly inner) nodes with specified members (\link{branches_attr_by_lists}) } |
| 97 |
\item{by_lists_branches_lty - set the line type of branches from the root of the tree down to (possibly inner) nodes with specified members (\link{branches_attr_by_lists}) } |
| 98 |
\item{highlight_branches_col - highlight branches color based on branches' heights (\link{highlight_branches_col}) } |
| 99 |
\item{highlight_branches_lwd - highlight branches line-width based on branches' heights (\link{highlight_branches_lwd}) } |
| 100 |
\item{clear_branches - clear branches' attributes (\link{remove_branches_edgePar})} |
| 101 |
\item{clear_leaves - clear leaves' attributes (\link{remove_branches_edgePar})} |
| 102 |
} |
| 103 |
} |
| 104 |
\examples{ |
| 105 |
|
| 106 |
\dontrun{ |
| 107 |
|
| 108 |
set.seed(23235) |
| 109 |
ss <- sample(1:150, 10) |
| 110 |
|
| 111 |
# Getting the dend object |
| 112 |
dend <- iris[ss, -5] \%>\% |
| 113 |
dist() \%>\% |
| 114 |
hclust() \%>\% |
| 115 |
as.dendrogram() |
| 116 |
dend \%>\% plot() |
| 117 |
|
| 118 |
dend \%>\% labels() |
| 119 |
dend \%>\% |
| 120 |
set("labels", 1:10) \%>\% |
| 121 |
labels() |
| 122 |
dend \%>\% |
| 123 |
set("labels", 1:10) \%>\% |
| 124 |
plot() |
| 125 |
dend \%>\% |
| 126 |
set("labels_color") \%>\% |
| 127 |
plot() |
| 128 |
dend \%>\% |
| 129 |
set("labels_col", c(1, 2)) \%>\% |
| 130 |
plot() # Works also with partial matching :) |
| 131 |
dend \%>\% |
| 132 |
set("labels_cex", c(1, 1.2)) \%>\% |
| 133 |
plot() |
| 134 |
dend \%>\% |
| 135 |
set("leaves_pch", NA) \%>\% |
| 136 |
plot() |
| 137 |
dend \%>\% |
| 138 |
set("leaves_pch", c(1:5)) \%>\% |
| 139 |
plot() |
| 140 |
dend \%>\% |
| 141 |
set("leaves_pch", c(19, 19, NA)) \%>\% |
| 142 |
set("leaves_cex", c(1, 2)) \%>\% |
| 143 |
plot() |
| 144 |
dend \%>\% |
| 145 |
set("leaves_pch", c(19, 19, NA)) \%>\% |
| 146 |
set("leaves_cex", c(1, 2)) \%>\% |
| 147 |
set("leaves_col", c(1, 1, 2, 2)) \%>\% |
| 148 |
plot() |
| 149 |
dend \%>\% |
| 150 |
set("hang") \%>\% |
| 151 |
plot() |
| 152 |
|
| 153 |
# using bg for leaves and nodes |
| 154 |
|
| 155 |
set.seed(23235) |
| 156 |
ss <- sample(1:150, 25) |
| 157 |
|
| 158 |
# Getting the dend object |
| 159 |
dend25 <- iris[ss, -5] \%>\% |
| 160 |
dist() \%>\% |
| 161 |
hclust() \%>\% |
| 162 |
as.dendrogram() |
| 163 |
|
| 164 |
dend25 \%>\% |
| 165 |
set("labels", 1:25) \%>\% |
| 166 |
set("nodes_pch", 21) \%>\% # set all nodes to be pch 21 |
| 167 |
set("nodes_col", "darkred") \%>\% |
| 168 |
set("nodes_bg", "gold") \%>\% |
| 169 |
set("leaves_pch", 1:25) \%>\% # Change the leaves pch to move from 1 to 25 |
| 170 |
set("leaves_col", "darkred") \%>\% |
| 171 |
set("leaves_bg", "gold") \%>\% |
| 172 |
plot(main = "pch 21 to 25 supports the\nnodes_bg and leaves_bg parameters") |
| 173 |
|
| 174 |
|
| 175 |
dend \%>\% |
| 176 |
set("branches_k_col") \%>\% |
| 177 |
plot() |
| 178 |
dend \%>\% |
| 179 |
set("branches_k_col", c(1, 2)) \%>\% |
| 180 |
plot() |
| 181 |
dend \%>\% |
| 182 |
set("branches_k_col", c(1, 2, 3), k = 3) \%>\% |
| 183 |
plot() |
| 184 |
dend \%>\% |
| 185 |
set("branches_k_col", k = 3) \%>\% |
| 186 |
plot() |
| 187 |
|
| 188 |
dend \%>\% |
| 189 |
set("branches_k_lty", k = 3) \%>\% |
| 190 |
plot() |
| 191 |
dend \%>\% |
| 192 |
set("branches_k_col", k = 3) \%>\% |
| 193 |
set("branches_k_lty", k = 3) \%>\% |
| 194 |
plot() |
| 195 |
|
| 196 |
dend \%>\% |
| 197 |
set("branches_col", c(1, 2, 1, 2, NA)) \%>\% |
| 198 |
plot() |
| 199 |
dend \%>\% |
| 200 |
set("branches_lwd", c(2, 1, 2)) \%>\% |
| 201 |
plot() |
| 202 |
dend \%>\% |
| 203 |
set("branches_lty", c(1, 2, 1)) \%>\% |
| 204 |
plot() |
| 205 |
|
| 206 |
# clears all of the things added to the leaves |
| 207 |
dend \%>\% |
| 208 |
set("labels_color", c(19, 19, NA)) \%>\% |
| 209 |
set("leaves_pch", c(19, 19, NA)) \%>\% # plot |
| 210 |
set("clear_leaves") \%>\% # remove all of what was done until this point |
| 211 |
plot() |
| 212 |
# Different order |
| 213 |
dend \%>\% |
| 214 |
set("leaves_pch", c(19, 19, NA)) \%>\% |
| 215 |
set("labels_color", c(19, 19, NA)) \%>\% |
| 216 |
set("clear_leaves") \%>\% |
| 217 |
plot() |
| 218 |
|
| 219 |
|
| 220 |
# doing this without chaining (\%>\%) will NOT be fun: |
| 221 |
dend \%>\% |
| 222 |
set("labels", 1:10) \%>\% |
| 223 |
set("labels_color") \%>\% |
| 224 |
set("branches_col", c(1, 2, 1, 2, NA)) \%>\% |
| 225 |
set("branches_lwd", c(2, 1, 2)) \%>\% |
| 226 |
set("branches_lty", c(1, 2, 1)) \%>\% |
| 227 |
set("hang") \%>\% |
| 228 |
plot() |
| 229 |
|
| 230 |
par(mfrow = c(1, 3)) |
| 231 |
dend \%>\% |
| 232 |
set("highlight_branches_col") \%>\% |
| 233 |
plot() |
| 234 |
dend \%>\% |
| 235 |
set("highlight_branches_lwd") \%>\% |
| 236 |
plot() |
| 237 |
dend \%>\% |
| 238 |
set("highlight_branches_col") \%>\% |
| 239 |
set("highlight_branches_lwd") \%>\% |
| 240 |
plot() |
| 241 |
par(mfrow = c(1, 1)) |
| 242 |
|
| 243 |
#---------------------------- |
| 244 |
# Examples for: by_labels_branches_col, by_labels_branches_lwd, by_labels_branches_lty |
| 245 |
|
| 246 |
old_labels <- labels(dend) |
| 247 |
dend \%>\% |
| 248 |
set("labels", seq_len(nleaves(dend))) \%>\% |
| 249 |
set("by_labels_branches_col", value = c(1:4, 7), TF_values = 'gold') \%>\% |
| 250 |
set("by_labels_branches_lwd", value = c(1:4, 7), TF_values = 5) \%>\% |
| 251 |
set("by_labels_branches_lty", value = c(1:4, 7), TF_values = 2) \%>\% |
| 252 |
set("labels", old_labels) \%>\% |
| 253 |
plot() |
| 254 |
|
| 255 |
dend \%>\% |
| 256 |
set("labels", seq_len(nleaves(dend))) \%>\% |
| 257 |
set("by_labels_branches_col", c(1:4, 7), type = "any", TF_values = c(4, 2)) \%>\% |
| 258 |
set("by_labels_branches_lwd", c(1:4, 7), type = "all", TF_values = c(4, 1)) \%>\% |
| 259 |
set("by_labels_branches_lty", c(1:4, 7), TF_values = c(4, 1)) \%>\% |
| 260 |
plot() |
| 261 |
|
| 262 |
#---- using order_value |
| 263 |
# This is probably not what you want, since cutree |
| 264 |
# returns clusters in the order of the original data: |
| 265 |
dend \%>\% |
| 266 |
set("labels_colors", cutree(dend, k = 3)) \%>\% |
| 267 |
plot() |
| 268 |
# The way to fix it, is to use order_value = TRUE |
| 269 |
# so that value is assumed to be in the order of the data: |
| 270 |
dend \%>\% |
| 271 |
set("labels_colors", cutree(dend, k = 3), order_value = TRUE) \%>\% |
| 272 |
plot() |
| 273 |
|
| 274 |
|
| 275 |
#---------------------------- |
| 276 |
# Example for: by_lists_branches_col, by_lists_branches_lwd, by_lists_branches_lty |
| 277 |
|
| 278 |
L <- list(c("109", "123", "126", "145"), "29", c("59", "67", "97")) |
| 279 |
dend \%>\% |
| 280 |
set("by_lists_branches_col", L, TF_value = "blue") \%>\% |
| 281 |
set("by_lists_branches_lwd", L, TF_value = 4) \%>\% |
| 282 |
set("by_lists_branches_lty", L, TF_value = 3) \%>\% |
| 283 |
plot() |
| 284 |
|
| 285 |
|
| 286 |
#---------------------------- |
| 287 |
# A few dendlist examples: |
| 288 |
dendlist(dend, dend) \%>\% |
| 289 |
set("hang") \%>\% |
| 290 |
plot() |
| 291 |
dendlist(dend, dend) \%>\% |
| 292 |
set("branches_k_col", k = 3) \%>\% |
| 293 |
plot() |
| 294 |
dendlist(dend, dend) \%>\% |
| 295 |
set("labels_col", c(1, 2)) \%>\% |
| 296 |
plot() |
| 297 |
|
| 298 |
dendlist(dend, dend) \%>\% |
| 299 |
set("hang") \%>\% |
| 300 |
set("labels_col", c(1, 2), which = 1) \%>\% |
| 301 |
set("branches_k_col", k = 3, which = 2) \%>\% |
| 302 |
set("labels_cex", 1.2) \%>\% |
| 303 |
plot() |
| 304 |
|
| 305 |
|
| 306 |
#---------------------------- |
| 307 |
# example of modifying the dendrogram in a heatmap: |
| 308 |
|
| 309 |
library(gplots) |
| 310 |
data(mtcars) |
| 311 |
x <- as.matrix(mtcars) |
| 312 |
rc <- rainbow(nrow(x), start = 0, end = .3) |
| 313 |
cc <- rainbow(ncol(x), start = 0, end = .3) |
| 314 |
|
| 315 |
## |
| 316 |
##' demonstrate the effect of row and column dendrogram options |
| 317 |
## |
| 318 |
Rowv_dend <- x \%>\% |
| 319 |
dist() \%>\% |
| 320 |
hclust() \%>\% |
| 321 |
as.dendrogram() \%>\% |
| 322 |
set("branches_k", k = 3) \%>\% |
| 323 |
set("branches_lwd", 2) \%>\% |
| 324 |
ladderize() # rotate_DendSer |
| 325 |
Colv_dend <- t(x) \%>\% |
| 326 |
dist() \%>\% |
| 327 |
hclust() \%>\% |
| 328 |
as.dendrogram() \%>\% |
| 329 |
set("branches_k", k = 3) \%>\% |
| 330 |
set("branches_lwd", 2) \%>\% |
| 331 |
ladderize() # rotate_DendSer |
| 332 |
heatmap.2(x, Rowv = Rowv_dend, Colv = Colv_dend) |
| 333 |
} |
| 334 |
} |
| 335 |
\seealso{ |
| 336 |
\link{labels<-.dendrogram}, \link{labels_colors<-}, |
| 337 |
\link{hang.dendrogram}, \link{color_branches}, |
| 338 |
\link{assign_values_to_leaves_nodePar}, |
| 339 |
\link{assign_values_to_branches_edgePar}, |
| 340 |
\link{remove_branches_edgePar}, \link{remove_leaves_nodePar}, |
| 341 |
\link{noded_with_condition}, \link{branches_attr_by_labels}, \link{branches_attr_by_lists}, |
| 342 |
\link{dendrogram} |
| 343 |
} |