| 1 |
## ----echo = FALSE, message = FALSE-------------------------------------------- |
| 2 |
library(dendextend) |
| 3 |
library(knitr) |
| 4 |
knitr::opts_chunk$set( |
| 5 |
cache = TRUE, |
| 6 |
dpi = 75, |
| 7 |
fig.width = 6, fig.height = 6, |
| 8 |
comment = "#>", |
| 9 |
tidy = FALSE) |
| 10 |
|
| 11 |
# https://stackoverflow.com/questions/24091735/why-pandoc-does-not-retrieve-the-image-file |
| 12 |
# < ! -- rmarkdown v1 --> |
| 13 |
|
| 14 |
|
| 15 |
## ----echo=FALSE, warning=FALSE, fig.align='center', fig.width=7, fig.height=7---- |
| 16 |
suppressMessages(library(dendextend)) |
| 17 |
library(colorspace) |
| 18 |
|
| 19 |
dend1 <- c(1:5) %>% # take some data |
| 20 |
dist %>% # calculate a distance matrix, |
| 21 |
hclust(method = "average") %>% # on it compute hierarchical clustering using the "average" method, |
| 22 |
as.dendrogram # and lastly, turn that object into a dendrogram. |
| 23 |
dend2 <- c(1:5) %>% # take some data |
| 24 |
dist %>% # calculate a distance matrix, |
| 25 |
hclust(method = "single") %>% # on it compute hierarchical clustering using the "average" method, |
| 26 |
as.dendrogram # and lastly, turn that object into a dendrogram. |
| 27 |
dend1 <- rotate(dend1, order = c(as.character(5:1))) |
| 28 |
labels(dend1) <- rev(c("dendextend", "let's u easily", "control","(and compare) your", "dendrograms")) |
| 29 |
labels(dend2) <- rev(c("let's u easily","dendextend" ,"control","(and compare) your", "dendrograms")) |
| 30 |
dend1 <- dend1 %>% |
| 31 |
color_labels %>% |
| 32 |
set("labels_cex", c(2.2,1.4)) %>% |
| 33 |
set("branches_lwd", c(4,1,4)) %>% |
| 34 |
set("branches_lty", c(1,2,1)) %>% |
| 35 |
set("nodes_pch", c(NA,19,NA)) %>% |
| 36 |
set("nodes_cex", c(NA,2.5,NA)) %>% |
| 37 |
set("nodes_col", c(NA,3,NA)) %>% |
| 38 |
hang.dendrogram # %>% plot |
| 39 |
dend2 <- color_branches(dend2) |
| 40 |
# dend2 <- color_labels(dend2) |
| 41 |
tanglegram(dendlist(dend1, dend2), margin_inner = 9, |
| 42 |
color_lines = c(rep("darkgreen", 3) , rep("darkred",2)), |
| 43 |
sub= paste("Entanglement:", round(entanglement(dendlist(dend1, dend2)),2)), cex_sub = 1.5 |
| 44 |
) |
| 45 |
|
| 46 |
# dend2 %>% color_branches %>% plot |
| 47 |
# dend2 %>% color_branches(k=3) %>% plot # nice, returns the tree as is... |
| 48 |
# dend2 %>% color_labels %>% plot |
| 49 |
# cutree(dend2,3) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
## ----echo=FALSE--------------------------------------------------------------- |
| 54 |
|
| 55 |
c(person("Tal", "Galili", role = c("aut", "cre", "cph"), email = |
| 56 |
"[email protected]", comment = "https://www.r-statistics.com"), |
| 57 |
person("Gavin", "Simpson", role = "ctb"), person("Gregory","Jefferis", role |
| 58 |
= "ctb", email = "[email protected]", |
| 59 |
comment ="imported code from his dendroextras package"), |
| 60 |
person("Marco", "Gallotta", role = "ctb", comment = |
| 61 |
"a.k.a: marcog") , person("Johan", "Renaudie", role = "ctb", comment = |
| 62 |
"https://github.com/plannapus"), person("R core team", role = "ctb", |
| 63 |
comment = "Thanks for the Infastructure, and code in the examples"), |
| 64 |
person("Kurt", "Hornik", role = "ctb"), person("Uwe", "Ligges", |
| 65 |
role = "ctb"), person("Andrej-Nikolai", "Spiess", role = "ctb"), |
| 66 |
person("Steve", "Horvath",email = "[email protected]", role = |
| 67 |
"ctb"), person("Peter", "Langfelder",email = "[email protected]", |
| 68 |
role = "ctb"), person("skullkey", role = "ctb"), person("Mark", |
| 69 |
"Van Der Loo", email = "[email protected]", comment = |
| 70 |
"https://github.com/markvanderloo d3dendrogram", role = "ctb"), |
| 71 |
person("Yoav", "Benjamini", role = "ths")) |
| 72 |
|
| 73 |
|
| 74 |
## ----eval = FALSE------------------------------------------------------------- |
| 75 |
# d1 <- c(1:5) # some data |
| 76 |
# d2 <- dist(d1) |
| 77 |
# d3 <- hclust(d2, method = "average") |
| 78 |
# dend <- as.dendrogram(d3) |
| 79 |
|
| 80 |
## ----eval=FALSE--------------------------------------------------------------- |
| 81 |
# dend <- as.dendrogram(hclust(dist(c(1:5)), method = "average")) |
| 82 |
|
| 83 |
## ----eval = FALSE------------------------------------------------------------- |
| 84 |
# dend <- c(1:5) %>% # take the a vector from 1 to 5 |
| 85 |
# dist %>% # calculate a distance matrix, |
| 86 |
# hclust(method = "average") %>% # on it compute hierarchical clustering using the "average" method, |
| 87 |
# as.dendrogram # and lastly, turn that object into a dendrogram. |
| 88 |
|
| 89 |
## ----fig.width=4, fig.height=3------------------------------------------------ |
| 90 |
# Create a dend: |
| 91 |
dend <- 1:2 %>% dist %>% hclust %>% as.dendrogram |
| 92 |
# and plot it: |
| 93 |
dend %>% plot |
| 94 |
|
| 95 |
## ----------------------------------------------------------------------------- |
| 96 |
dend %>% unclass %>% str |
| 97 |
dend %>% class |
| 98 |
|
| 99 |
## ----fig.width=4, fig.height=3------------------------------------------------ |
| 100 |
# Create a dend: |
| 101 |
dend <- 1:5 %>% dist %>% hclust %>% as.dendrogram |
| 102 |
# Plot it: |
| 103 |
dend %>% plot |
| 104 |
|
| 105 |
## ----------------------------------------------------------------------------- |
| 106 |
dend %>% labels # get the labels of the tree |
| 107 |
dend %>% nleaves # get the number of leaves of the tree |
| 108 |
dend %>% nnodes # get the number of nodes in the tree (including leaves) |
| 109 |
dend %>% head # A combination of "str" with "head" |
| 110 |
|
| 111 |
## ----echo=FALSE, fig.height=5------------------------------------------------- |
| 112 |
# Create a dend: |
| 113 |
dend <- 1:5 %>% dist %>% hclust %>% as.dendrogram |
| 114 |
|
| 115 |
# get_nodes_xy(dend) |
| 116 |
# polygon(get_nodes_xy(dend), col = 2) |
| 117 |
plot(dend, |
| 118 |
leaflab = "none", # axes = FALSE, # no labels or y axis |
| 119 |
main = "Nodes order when using \nDepth-first search in a dendrogram") |
| 120 |
xy <- get_nodes_xy(dend) |
| 121 |
for(i in 1:(nrow(xy)-1)) { |
| 122 |
arrows(xy[i,1], xy[i,2], angle = 17, |
| 123 |
length = .3, |
| 124 |
xy[i+1,1], xy[i+1,2], |
| 125 |
lty = 1, col = 3, lwd = 1.5) |
| 126 |
} |
| 127 |
points(xy, pch = 19, cex = 3) |
| 128 |
text(xy, labels = 1:nnodes(dend),cex = 1.2, col = "white") #, adj = c(0.4,0.4)) |
| 129 |
|
| 130 |
## ----------------------------------------------------------------------------- |
| 131 |
# Create a dend: |
| 132 |
dend <- 1:5 %>% dist %>% hclust %>% as.dendrogram |
| 133 |
# Get various attributes |
| 134 |
dend %>% get_nodes_attr("height") # node's height |
| 135 |
dend %>% hang.dendrogram %>% get_nodes_attr("height") # node's height (after raising the leaves) |
| 136 |
dend %>% get_nodes_attr("members") # number of members (leaves) under that node |
| 137 |
dend %>% get_nodes_attr("members", id = c(2,5)) # number of members for nodes 2 and 5 |
| 138 |
dend %>% get_nodes_attr("midpoint") # how much "left" is this node from its left-most child's location |
| 139 |
dend %>% get_nodes_attr("leaf") # is this node a leaf |
| 140 |
dend %>% get_nodes_attr("label") # what is the label on this node |
| 141 |
dend %>% get_nodes_attr("nodePar") # empty (for now...) |
| 142 |
dend %>% get_nodes_attr("edgePar") # empty (for now...) |
| 143 |
|
| 144 |
## ----fig.show='hold', fig.width=8, fig.height=3------------------------------- |
| 145 |
dend13 <- c(1:3) %>% # take some data |
| 146 |
dist %>% # calculate a distance matrix, |
| 147 |
hclust(method = "average") %>% # on it compute hierarchical clustering using the "average" method, |
| 148 |
as.dendrogram # and lastly, turn that object into a dendrogram. |
| 149 |
# same, but for 5 leaves: |
| 150 |
dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram |
| 151 |
|
| 152 |
par(mfrow = c(1,2)) |
| 153 |
dend13 %>% plot(main="dend13") |
| 154 |
dend15 %>% plot(main="dend15") |
| 155 |
# we could have also used plot(dend) |
| 156 |
|
| 157 |
## ----------------------------------------------------------------------------- |
| 158 |
# get the labels: |
| 159 |
dend15 %>% labels |
| 160 |
# this is just like labels(dend) |
| 161 |
|
| 162 |
## ----------------------------------------------------------------------------- |
| 163 |
# change the labels, and then print them: |
| 164 |
dend15 %>% set("labels", c(111:115)) %>% labels |
| 165 |
# could also be done using: |
| 166 |
# labels(dend) <- c(111:115) |
| 167 |
|
| 168 |
## ----------------------------------------------------------------------------- |
| 169 |
dend15 %>% labels |
| 170 |
dend15 %>% set("labels_to_char") %>% labels |
| 171 |
|
| 172 |
## ----fig.width=8, fig.height=3------------------------------------------------ |
| 173 |
par(mfrow = c(1,2)) |
| 174 |
dend15 %>% set("labels_col", "blue") %>% plot(main = "Change label's color") # change color |
| 175 |
dend15 %>% set("labels_cex", 2) %>% plot(main = "Change label's size") # change color |
| 176 |
|
| 177 |
## ----fig.width=8, fig.height=3------------------------------------------------ |
| 178 |
# Produce a more complex dendrogram: |
| 179 |
dend15_2 <- dend15 %>% |
| 180 |
set("labels", c(111:115)) %>% # change labels |
| 181 |
set("labels_col", c(1,2,3)) %>% # change color |
| 182 |
set("labels_cex", c(2,1)) # change size |
| 183 |
|
| 184 |
par(mfrow = c(1,2)) |
| 185 |
dend15 %>% plot(main = "Before") |
| 186 |
dend15_2 %>% plot(main = "After") |
| 187 |
|
| 188 |
## ----------------------------------------------------------------------------- |
| 189 |
# looking at only the left-most node of the "after tree": |
| 190 |
dend15_2[[1]][[1]] %>% unclass %>% str |
| 191 |
# looking at only the nodePar attributes in this sub-tree: |
| 192 |
dend15_2[[1]][[1]] %>% get_nodes_attr("nodePar") |
| 193 |
|
| 194 |
## ----fig.width=8, fig.height=3------------------------------------------------ |
| 195 |
par(mfrow = c(1,2)) |
| 196 |
dend15 %>% set("labels_cex", 2) %>% set("labels_col", value = c(3,4)) %>% |
| 197 |
plot(main = "Recycles color \nfrom left to right") |
| 198 |
dend15 %>% set("labels_cex", 2) %>% set("labels_col", value = c(3,4), k=2) %>% |
| 199 |
plot(main = "Color labels \nper cluster") |
| 200 |
abline(h = 2, lty = 2) |
| 201 |
|
| 202 |
## ----fig.width=10, fig.height=6----------------------------------------------- |
| 203 |
par(mfrow = c(2,3)) |
| 204 |
dend13 %>% set("nodes_pch", 19) %>% plot(main = "(1) Show the\n nodes (as a dot)") #1 |
| 205 |
dend13 %>% set("nodes_pch", 19) %>% set("nodes_cex", 2) %>% |
| 206 |
plot(main = "(2) Show (larger)\n nodes") #2 |
| 207 |
dend13 %>% set("nodes_pch", 19) %>% set("nodes_cex", 2) %>% set("nodes_col", 3) %>% |
| 208 |
plot(main = "(3) Show (larger+colored)\n nodes") #3 |
| 209 |
|
| 210 |
dend13 %>% set("leaves_pch", 21) %>% plot(main = "(4) Show the leaves\n (as empty circles)") #4 |
| 211 |
dend13 %>% set("leaves_pch", 21) %>% set("leaves_cex", 2) %>% |
| 212 |
plot(main = "(5) Show (larger)\n leaf circles") #5 |
| 213 |
dend13 %>% |
| 214 |
set("leaves_pch", 21) %>% |
| 215 |
set("leaves_bg", "gold") %>% |
| 216 |
set("leaves_cex", 2) %>% |
| 217 |
set("leaves_col", "darkred") %>% |
| 218 |
plot(main = "(6) Show (larger+colored+filled)\n leaves") #6 |
| 219 |
|
| 220 |
|
| 221 |
## ----fig.width=8, fig.height=4------------------------------------------------ |
| 222 |
par(mfrow = c(1,2)) |
| 223 |
dend15 %>% set("nodes_pch", c(19,1,4)) %>% set("nodes_cex", c(2,1,2)) %>% set("nodes_col", c(3,4)) %>% |
| 224 |
plot(main = "Adjust nodes") |
| 225 |
dend15 %>% set("leaves_pch", c(19,1,4)) %>% set("leaves_cex", c(2,1,2)) %>% set("leaves_col", c(3,4)) %>% |
| 226 |
plot(main = "Adjust nodes\n(but only for leaves)") |
| 227 |
|
| 228 |
## ----------------------------------------------------------------------------- |
| 229 |
dend15 %>% set("nodes_pch", c(19,1,4)) %>% |
| 230 |
set("nodes_cex", c(2,1,2)) %>% set("nodes_col", c(3,4)) %>% get_nodes_attr("nodePar") |
| 231 |
|
| 232 |
## ----fig.width=10, fig.height=3----------------------------------------------- |
| 233 |
par(mfrow = c(1,3)) |
| 234 |
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 2) %>% # adjust the leaves |
| 235 |
hang.dendrogram %>% # hang the leaves |
| 236 |
plot(main = "Hanging a tree") |
| 237 |
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 2) %>% # adjust the leaves |
| 238 |
hang.dendrogram(hang_height = .6) %>% # hang the leaves (at some height) |
| 239 |
plot(main = "Hanging a tree (but lower)") |
| 240 |
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 2) %>% # adjust the leaves |
| 241 |
hang.dendrogram %>% # hang the leaves |
| 242 |
hang.dendrogram(hang = -1) %>% # un-hanging the leaves |
| 243 |
plot(main = "Not hanging a tree") |
| 244 |
|
| 245 |
## ----------------------------------------------------------------------------- |
| 246 |
dend13 %>% get_leaves_attr("height") |
| 247 |
dend13 %>% hang.dendrogram %>% get_leaves_attr("height") |
| 248 |
|
| 249 |
## ----fig.width=10, fig.height=3----------------------------------------------- |
| 250 |
par(mfrow = c(1,3)) |
| 251 |
dend13 %>% plot(main = "First tree", ylim = c(0,3)) |
| 252 |
dend13 %>% |
| 253 |
raise.dendrogram (-1) %>% |
| 254 |
plot(main = "One point lower", ylim = c(0,3)) |
| 255 |
dend13 %>% |
| 256 |
raise.dendrogram (1) %>% |
| 257 |
plot(main = "One point higher", ylim = c(0,3)) |
| 258 |
|
| 259 |
## ----fig.width=10, fig.height=3----------------------------------------------- |
| 260 |
par(mfrow = c(1,3)) |
| 261 |
dend13 %>% set("branches_lwd", 4) %>% plot(main = "Thick branches") |
| 262 |
dend13 %>% set("branches_lty", 3) %>% plot(main = "Dashed branches") |
| 263 |
dend13 %>% set("branches_col", 2) %>% plot(main = "Red branches") |
| 264 |
|
| 265 |
## ----fig.width=4, fig.height=3------------------------------------------------ |
| 266 |
# Produce a more complex dendrogram: |
| 267 |
dend15 %>% |
| 268 |
set("branches_lwd", c(4,1)) %>% |
| 269 |
set("branches_lty", c(1,1,3)) %>% |
| 270 |
set("branches_col", c(1,2,3)) %>% |
| 271 |
plot(main = "Complex branches", edge.root = TRUE) |
| 272 |
|
| 273 |
## ----fig.width=8, fig.height=3------------------------------------------------ |
| 274 |
par(mfrow = c(1,2)) |
| 275 |
dend15 %>% set("branches_k_color", k = 3) %>% plot(main = "Nice defaults") |
| 276 |
dend15 %>% set("branches_k_color", value = 3:1, k = 3) %>% |
| 277 |
plot(main = "Controlling branches' colors\n(via clustering)") |
| 278 |
# This is like using the `color_branches` function |
| 279 |
|
| 280 |
## ----fig.width=8, fig.height=3------------------------------------------------ |
| 281 |
par(mfrow = c(1,2)) |
| 282 |
dend15 %>% set("by_labels_branches_col", value = c(1,4)) %>% |
| 283 |
plot(main = "Adjust the branch\n if ALL (default) of its\n labels are in the list") |
| 284 |
dend15 %>% set("by_labels_branches_col", value = c(1,4), type = "any") %>% |
| 285 |
plot(main = "Adjust the branch\n if ANY of its\n labels are in the list") |
| 286 |
|
| 287 |
## ----fig.width=10, fig.height=3----------------------------------------------- |
| 288 |
# Using "Inf" in "TF_values" means to let the parameters stay as they are. |
| 289 |
par(mfrow = c(1,3)) |
| 290 |
dend15 %>% set("by_labels_branches_col", value = c(1,4), TF_values = c(3,Inf)) %>% |
| 291 |
plot(main = "Change colors") |
| 292 |
dend15 %>% set("by_labels_branches_lwd", value = c(1,4), TF_values = c(8,1)) %>% |
| 293 |
plot(main = "Change line width") |
| 294 |
dend15 %>% set("by_labels_branches_lty", value = c(1,4), TF_values = c(3,Inf)) %>% |
| 295 |
plot(main = "Change line type") |
| 296 |
|
| 297 |
## ----fig.width=8, fig.height=3------------------------------------------------ |
| 298 |
|
| 299 |
dat <- iris[1:20,-5] |
| 300 |
hca <- hclust(dist(dat)) |
| 301 |
hca2 <- hclust(dist(dat), method = "single") |
| 302 |
dend <- as.dendrogram(hca) |
| 303 |
dend2 <- as.dendrogram(hca2) |
| 304 |
|
| 305 |
par(mfrow = c(1,3)) |
| 306 |
dend %>% highlight_branches_col %>% plot(main = "Coloring branches") |
| 307 |
dend %>% highlight_branches_lwd %>% plot(main = "Emphasizing line-width") |
| 308 |
dend %>% highlight_branches %>% plot(main = "Emphasizing color\n and line-width") |
| 309 |
|
| 310 |
|
| 311 |
## ----fig.width=8, fig.height=4------------------------------------------------ |
| 312 |
|
| 313 |
library(viridis) |
| 314 |
par(mfrow = c(1,3)) |
| 315 |
dend %>% highlight_branches_col %>% plot(main = "Coloring branches \n (default is reversed viridis)") |
| 316 |
dend %>% highlight_branches_col(viridis(100)) %>% plot(main = "It is better to use \n lighter colors in the leaves") |
| 317 |
dend %>% highlight_branches_col(rev(magma(1000))) %>% plot(main = "The magma color pallatte\n is also good") |
| 318 |
|
| 319 |
dl <- dendlist(dend, dend2) |
| 320 |
tanglegram(dl, sort = TRUE, common_subtrees_color_lines = FALSE, highlight_distinct_edges = FALSE, highlight_branches_lwd = FALSE) |
| 321 |
tanglegram(dl) |
| 322 |
tanglegram(dl, fast = TRUE) |
| 323 |
|
| 324 |
dl <- dendlist(highlight_branches(dend), highlight_branches(dend2)) |
| 325 |
tanglegram(dl, sort = TRUE, common_subtrees_color_lines = FALSE, highlight_distinct_edges = FALSE) |
| 326 |
|
| 327 |
# dend %>% set("highlight_branches_col") %>% plot |
| 328 |
|
| 329 |
dl <- dendlist(dend, dend2) %>% set("highlight_branches_col") |
| 330 |
tanglegram(dl, sort = TRUE, common_subtrees_color_lines = FALSE, highlight_distinct_edges = FALSE) |
| 331 |
|
| 332 |
|
| 333 |
## ----fig.width=10, fig.height=3----------------------------------------------- |
| 334 |
par(mfrow = c(1,3)) |
| 335 |
dend15 %>% |
| 336 |
set("labels_colors") %>% |
| 337 |
set("branches_k_color") %>% |
| 338 |
plot(main = "First tree") |
| 339 |
dend15 %>% |
| 340 |
set("labels_colors") %>% |
| 341 |
set("branches_k_color") %>% |
| 342 |
rotate(as.character(5:1)) %>% #rotate to match labels new order |
| 343 |
plot(main = "Rotated tree\n based on labels") |
| 344 |
dend15 %>% |
| 345 |
set("labels_colors") %>% |
| 346 |
set("branches_k_color") %>% |
| 347 |
rotate(5:1) %>% # the fifth label to go first is "4" |
| 348 |
plot(main = "Rotated tree\n based on order") |
| 349 |
|
| 350 |
## ----fig.width=12, fig.height=6----------------------------------------------- |
| 351 |
dend110 <- c(1, 3:5, 7,9,10) %>% dist %>% hclust(method = "average") %>% |
| 352 |
as.dendrogram %>% color_labels %>% color_branches |
| 353 |
|
| 354 |
par(mfrow = c(1,3)) |
| 355 |
dend110 %>% plot(main = "Original tree") |
| 356 |
dend110 %>% sort %>% plot(main = "labels sort") |
| 357 |
dend110 %>% sort(type = "nodes") %>% plot(main = "nodes (ladderize) sort") |
| 358 |
|
| 359 |
## ----fig.width=10, fig.height=3----------------------------------------------- |
| 360 |
par(mfrow = c(1,3)) |
| 361 |
dend15 %>% plot(main = "First tree", ylim = c(0,3)) |
| 362 |
dend15 %>% |
| 363 |
unbranch %>% |
| 364 |
plot(main = "Unbranched tree", ylim = c(0,3)) |
| 365 |
dend15 %>% |
| 366 |
unbranch(2) %>% |
| 367 |
plot(main = "Unbranched tree (2)", ylim = c(0,3)) |
| 368 |
|
| 369 |
## ----fig.width=7, fig.height=3------------------------------------------------ |
| 370 |
par(mfrow = c(1,2)) |
| 371 |
dend15 %>% set("labels_colors") %>% |
| 372 |
plot(main = "First tree", ylim = c(0,3)) |
| 373 |
dend15 %>% set("labels_colors") %>% |
| 374 |
prune(c("1","5")) %>% |
| 375 |
plot(main = "Prunned tree", ylim = c(0,3)) |
| 376 |
|
| 377 |
## ----fig.width=7, fig.height=3------------------------------------------------ |
| 378 |
par(mfrow = c(1,2)) |
| 379 |
dend_intersected <- intersect_trees(dend13, dend15) |
| 380 |
dend_intersected[[1]] %>% plot |
| 381 |
dend_intersected[[2]] %>% plot |
| 382 |
|
| 383 |
## ----------------------------------------------------------------------------- |
| 384 |
# ladderize is like sort(..., type = "node") |
| 385 |
dend <- iris[1:5,-5] %>% dist %>% hclust %>% as.dendrogram |
| 386 |
par(mfrow = c(1,3)) |
| 387 |
dend %>% ladderize %>% plot(horiz = TRUE); abline(v = .2, col = 2, lty = 2) |
| 388 |
dend %>% collapse_branch(tol = 0.2) %>% ladderize %>% plot(horiz = TRUE) |
| 389 |
dend %>% collapse_branch(tol = 0.2) %>% ladderize %>% hang.dendrogram(hang = 0) %>% plot(horiz = TRUE) |
| 390 |
|
| 391 |
## ----fig.width=6, fig.height=3------------------------------------------------ |
| 392 |
layout(t(c(1,1,1,2,2))) |
| 393 |
|
| 394 |
dend15 %>% set("branches_k_color") %>% plot |
| 395 |
dend15 %>% rect.dendrogram(k=3, |
| 396 |
border = 8, lty = 5, lwd = 2) |
| 397 |
|
| 398 |
dend15 %>% set("branches_k_color") %>% plot(horiz = TRUE) |
| 399 |
dend15 %>% rect.dendrogram(k=3, horiz = TRUE, |
| 400 |
border = 8, lty = 5, lwd = 2) |
| 401 |
|
| 402 |
|
| 403 |
## ----fig.width=4, fig.height=4------------------------------------------------ |
| 404 |
is_odd <- ifelse(labels(dend15) %% 2, 2,3) |
| 405 |
is_345 <- ifelse(labels(dend15) > 2, 3,4) |
| 406 |
is_12 <- ifelse(labels(dend15) <= 2, 3,4) |
| 407 |
k_3 <- cutree(dend15,k = 3, order_clusters_as_data = FALSE) |
| 408 |
# The FALSE above makes sure we get the clusters in the order of the |
| 409 |
# dendrogram, and not in that of the original data. It is like: |
| 410 |
# cutree(dend15, k = 3)[order.dendrogram(dend15)] |
| 411 |
the_bars <- cbind(is_odd, is_345, is_12, k_3) |
| 412 |
the_bars[the_bars==2] <- 8 |
| 413 |
|
| 414 |
dend15 %>% plot |
| 415 |
colored_bars(colors = the_bars, dend = dend15, sort_by_labels_order = FALSE) |
| 416 |
# we use sort_by_labels_order = FALSE since "the_bars" were set based on the |
| 417 |
# labels order. The more common use case is when the bars are based on a second variable |
| 418 |
# from the same data.frame as dend was created from. Thus, the default |
| 419 |
# sort_by_labels_order = TRUE would make more sense. |
| 420 |
|
| 421 |
## ----------------------------------------------------------------------------- |
| 422 |
|
| 423 |
dend_mtcars <- mtcars[, c("mpg", "disp")] %>% dist %>% hclust(method = "average") %>% as.dendrogram |
| 424 |
|
| 425 |
par(mar = c(10,2,1,1)) |
| 426 |
plot(dend_mtcars) |
| 427 |
the_bars <- ifelse(mtcars$am, "grey", "gold") |
| 428 |
colored_bars(colors = the_bars, dend = dend_mtcars, rowLabels = "am") |
| 429 |
|
| 430 |
|
| 431 |
## ----------------------------------------------------------------------------- |
| 432 |
# Create a complex dend: |
| 433 |
dend <- iris[1:30,-5] %>% dist %>% hclust %>% as.dendrogram %>% |
| 434 |
set("branches_k_color", k=3) %>% set("branches_lwd", c(1.5,1,1.5)) %>% |
| 435 |
set("branches_lty", c(1,1,3,1,1,2)) %>% |
| 436 |
set("labels_colors") %>% set("labels_cex", c(.9,1.2)) %>% |
| 437 |
set("nodes_pch", 19) %>% set("nodes_col", c("orange", "black", "plum", NA)) |
| 438 |
# plot the dend in usual "base" plotting engine: |
| 439 |
plot(dend) |
| 440 |
# Now let's do it in ggplot2 :) |
| 441 |
ggd1 <- as.ggdend(dend) |
| 442 |
library(ggplot2) |
| 443 |
# the nodes are not implemented yet. |
| 444 |
ggplot(ggd1) # reproducing the above plot in ggplot2 :) |
| 445 |
ggplot(ggd1, horiz = TRUE, theme = NULL) # horiz plot (and let's remove theme) in ggplot2 |
| 446 |
# Adding some extra spice to it... |
| 447 |
# creating a radial plot: |
| 448 |
# ggplot(ggd1) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta="x") |
| 449 |
# The text doesn't look so great, so let's remove it: |
| 450 |
ggplot(ggd1, labels = FALSE) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta="x") |
| 451 |
|
| 452 |
## ----fig.width=7, fig.height=3------------------------------------------------ |
| 453 |
if(require(DendSer)) { |
| 454 |
par(mfrow = c(1,2)) |
| 455 |
DendSer.dendrogram(dend15) |
| 456 |
|
| 457 |
dend15 %>% color_branches %>% plot |
| 458 |
dend15 %>% color_branches %>% rotate_DendSer %>% plot |
| 459 |
} |
| 460 |
|
| 461 |
## ----message=FALSE, fig.width=7, fig.height=7--------------------------------- |
| 462 |
library(gplots) |
| 463 |
|
| 464 |
x <- as.matrix(datasets::mtcars) |
| 465 |
|
| 466 |
heatmap.2(x) |
| 467 |
|
| 468 |
# now let's spice up the dendrograms a bit: |
| 469 |
Rowv <- x %>% dist %>% hclust %>% as.dendrogram %>% |
| 470 |
set("branches_k_color", k = 3) %>% set("branches_lwd", 4) %>% |
| 471 |
ladderize |
| 472 |
# rotate_DendSer(ser_weight = dist(x)) |
| 473 |
Colv <- x %>% t %>% dist %>% hclust %>% as.dendrogram %>% |
| 474 |
set("branches_k_color", k = 2) %>% set("branches_lwd", 4) %>% |
| 475 |
ladderize |
| 476 |
# rotate_DendSer(ser_weight = dist(t(x))) |
| 477 |
|
| 478 |
heatmap.2(x, Rowv = Rowv, Colv = Colv) |
| 479 |
|
| 480 |
## ----message=FALSE, eval = FALSE---------------------------------------------- |
| 481 |
# |
| 482 |
# # library(NMF) |
| 483 |
# # |
| 484 |
# # x <- as.matrix(datasets::mtcars) |
| 485 |
# # |
| 486 |
# # # now let's spice up the dendrograms a bit: |
| 487 |
# # Rowv <- x %>% dist %>% hclust %>% as.dendrogram %>% |
| 488 |
# # set("branches_k_color", k = 3) %>% set("branches_lwd", 4) %>% |
| 489 |
# # ladderize |
| 490 |
# # # rotate_DendSer(ser_weight = dist(x)) |
| 491 |
# # Colv <- x %>% t %>% dist %>% hclust %>% as.dendrogram %>% |
| 492 |
# # set("branches_k_color", k = 2) %>% set("branches_lwd", 4) %>% |
| 493 |
# # ladderize |
| 494 |
# # # rotate_DendSer(ser_weight = dist(t(x))) |
| 495 |
# # |
| 496 |
# # aheatmap(x, Rowv = Rowv, Colv = Colv) |
| 497 |
# |
| 498 |
# |
| 499 |
# |
| 500 |
|
| 501 |
## ----------------------------------------------------------------------------- |
| 502 |
x <- as.matrix(datasets::mtcars) |
| 503 |
# heatmaply(x) |
| 504 |
# now let's spice up the dendrograms a bit: |
| 505 |
Rowv <- x %>% dist %>% hclust %>% as.dendrogram %>% |
| 506 |
set("branches_k_color", k = 3) %>% set("branches_lwd", 4) %>% |
| 507 |
ladderize |
| 508 |
# rotate_DendSer(ser_weight = dist(x)) |
| 509 |
Colv <- x %>% t %>% dist %>% hclust %>% as.dendrogram %>% |
| 510 |
set("branches_k_color", k = 2) %>% set("branches_lwd", 4) %>% |
| 511 |
ladderize |
| 512 |
# rotate_DendSer(ser_weight = dist(t(x))) |
| 513 |
|
| 514 |
## ----message=FALSE, cache = FALSE, eval = FALSE------------------------------- |
| 515 |
# library(heatmaply) |
| 516 |
# heatmaply(x, Rowv = Rowv, Colv = Colv) |
| 517 |
|
| 518 |
## ----------------------------------------------------------------------------- |
| 519 |
# let's get the clusters |
| 520 |
library(dynamicTreeCut) |
| 521 |
data(iris) |
| 522 |
x <- iris[,-5] %>% as.matrix |
| 523 |
hc <- x %>% dist %>% hclust |
| 524 |
dend <- hc %>% as.dendrogram |
| 525 |
|
| 526 |
# Find special clusters: |
| 527 |
clusters <- cutreeDynamic(hc, distM = as.matrix(dist(x)), method = "tree") |
| 528 |
# we need to sort them to the order of the dendrogram: |
| 529 |
clusters <- clusters[order.dendrogram(dend)] |
| 530 |
clusters_numbers <- unique(clusters) - (0 %in% clusters) |
| 531 |
n_clusters <- length(clusters_numbers) |
| 532 |
|
| 533 |
library(colorspace) |
| 534 |
cols <- rainbow_hcl(n_clusters) |
| 535 |
true_species_cols <- rainbow_hcl(3)[as.numeric(iris[,][order.dendrogram(dend),5])] |
| 536 |
dend2 <- dend %>% |
| 537 |
branches_attr_by_clusters(clusters, values = cols) %>% |
| 538 |
color_labels(col = true_species_cols) |
| 539 |
plot(dend2) |
| 540 |
clusters <- factor(clusters) |
| 541 |
levels(clusters)[-1] <- cols[-5][c(1,4,2,3)] |
| 542 |
# Get the clusters to have proper colors. |
| 543 |
# fix the order of the colors to match the branches. |
| 544 |
colored_bars(clusters, dend, sort_by_labels_order = FALSE) |
| 545 |
# here we used sort_by_labels_order = FALSE since the clusters were already sorted based on the dendrogram's order |
| 546 |
|
| 547 |
|
| 548 |
## ----message=FALSE, fig.width=9, results='hide'------------------------------- |
| 549 |
par(mfrow = c(1,2)) |
| 550 |
|
| 551 |
library(pvclust) |
| 552 |
data(lung) # 916 genes for 73 subjects |
| 553 |
set.seed(13134) |
| 554 |
result <- pvclust(lung[1:100, 1:10], |
| 555 |
method.dist="cor", method.hclust="average", nboot=10) |
| 556 |
|
| 557 |
# with pvrect |
| 558 |
plot(result) |
| 559 |
pvrect(result) |
| 560 |
|
| 561 |
# with a dendrogram of pvrect |
| 562 |
dend <- as.dendrogram(result) |
| 563 |
result %>% as.dendrogram %>% |
| 564 |
plot(main = "Cluster dendrogram with AU/BP values (%)\n reproduced plot with dendrogram") |
| 565 |
result %>% text |
| 566 |
result %>% pvrect |
| 567 |
|
| 568 |
## ----fig.height=8, fig.width=8------------------------------------------------ |
| 569 |
par(mfrow = c(2,2)) |
| 570 |
|
| 571 |
# with a modified dendrogram of pvrect |
| 572 |
dend %>% pvclust_show_signif(result) %>% |
| 573 |
plot(main = "Cluster dendrogram \n bp values are highlighted by signif") |
| 574 |
|
| 575 |
dend %>% pvclust_show_signif(result, show_type = "lwd") %>% |
| 576 |
plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are highlighted by signif") |
| 577 |
result %>% text |
| 578 |
result %>% pvrect(alpha=0.95) |
| 579 |
|
| 580 |
|
| 581 |
dend %>% pvclust_show_signif_gradient(result) %>% |
| 582 |
plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are colored by signif") |
| 583 |
|
| 584 |
dend %>% |
| 585 |
pvclust_show_signif_gradient(result) %>% |
| 586 |
pvclust_show_signif(result) %>% |
| 587 |
plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are colored+highlighted by signif") |
| 588 |
result %>% text |
| 589 |
result %>% pvrect(alpha=0.95) |
| 590 |
|
| 591 |
## ----------------------------------------------------------------------------- |
| 592 |
library(circlize) |
| 593 |
|
| 594 |
dend <- iris[1:40,-5] %>% dist %>% hclust %>% as.dendrogram %>% |
| 595 |
set("branches_k_color", k=3) %>% set("branches_lwd", c(5,2,1.5)) %>% |
| 596 |
set("branches_lty", c(1,1,3,1,1,2)) %>% |
| 597 |
set("labels_colors") %>% set("labels_cex", c(.6,1.5)) %>% |
| 598 |
set("nodes_pch", 19) %>% set("nodes_col", c("orange", "black", "plum", NA)) |
| 599 |
|
| 600 |
par(mar = rep(0,4)) |
| 601 |
circlize_dendrogram(dend) |
| 602 |
# circlize_dendrogram(dend, labels = FALSE) |
| 603 |
# circlize_dendrogram(dend, facing = "inside", labels = FALSE) |
| 604 |
|
| 605 |
## ----------------------------------------------------------------------------- |
| 606 |
# dend <- iris[1:40,-5] %>% dist %>% hclust %>% as.dendrogram %>% |
| 607 |
# set("branches_k_color", k=3) %>% set("branches_lwd", c(5,2,1.5)) %>% |
| 608 |
# set("branches_lty", c(1,1,3,1,1,2)) %>% |
| 609 |
# set("labels_colors") %>% set("labels_cex", c(.9,1.2)) %>% |
| 610 |
# set("nodes_pch", 19) %>% set("nodes_col", c("orange", "black", "plum", NA)) |
| 611 |
|
| 612 |
set.seed(2015-07-10) |
| 613 |
# In the following we get the dendrogram but can also get extra information on top of it |
| 614 |
circos.initialize("foo", xlim = c(0, 40)) |
| 615 |
circos.track(ylim = c(0, 1), panel.fun = function(x, y) { |
| 616 |
circos.rect(1:40-0.8, rep(0, 40), 1:40-0.2, runif(40), col = rand_color(40), border = NA) |
| 617 |
}, bg.border = NA) |
| 618 |
circos.track(ylim = c(0, 1), panel.fun = function(x, y) { |
| 619 |
circos.text(1:40-0.5, rep(0, 40), labels(dend), col = labels_colors(dend), |
| 620 |
facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5)) |
| 621 |
}, bg.border = NA, track.height = 0.1) |
| 622 |
max_height = attr(dend, "height") |
| 623 |
circos.track(ylim = c(0, max_height), panel.fun = function(x, y) { |
| 624 |
circos.dendrogram(dend, max_height = max_height) |
| 625 |
}, track.height = 0.5, bg.border = NA) |
| 626 |
circos.clear() |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
## ----------------------------------------------------------------------------- |
| 631 |
dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram |
| 632 |
dend15 <- dend15 %>% set("labels_to_char") |
| 633 |
dend51 <- dend15 %>% set("labels", as.character(5:1)) %>% match_order_by_labels(dend15) |
| 634 |
dends_15_51 <- dendlist(dend15, dend51) |
| 635 |
dends_15_51 |
| 636 |
head(dends_15_51) |
| 637 |
|
| 638 |
## ----------------------------------------------------------------------------- |
| 639 |
# example 1 |
| 640 |
x <- 1:5 %>% dist %>% hclust %>% as.dendrogram |
| 641 |
y <- set(x, "labels", 5:1) |
| 642 |
|
| 643 |
# example 2 |
| 644 |
dend1 <- 1:10 %>% dist %>% hclust %>% as.dendrogram |
| 645 |
dend2 <- dend1 %>% set("labels", c(1,3,2,4, 5:10) ) |
| 646 |
dend_diff(dend1, dend2) |
| 647 |
|
| 648 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 649 |
tanglegram(dends_15_51) |
| 650 |
# Same as using: |
| 651 |
# plot(dends_15_51) # since there is a plot method for dendlist |
| 652 |
# and also: |
| 653 |
# tanglegram(dend15, dend51) |
| 654 |
|
| 655 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 656 |
tanglegram(dends_15_51, common_subtrees_color_branches = TRUE) |
| 657 |
|
| 658 |
## ----------------------------------------------------------------------------- |
| 659 |
dends_15_51 %>% entanglement # lower is better |
| 660 |
# dends_15_51 %>% untangle(method = "DendSer") %>% entanglement # lower is better |
| 661 |
dends_15_51 %>% untangle(method = "step1side") %>% entanglement # lower is better |
| 662 |
|
| 663 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 664 |
dends_15_51 %>% untangle(method = "step1side") %>% |
| 665 |
tanglegram(common_subtrees_color_branches = TRUE) |
| 666 |
|
| 667 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 668 |
x <- dends_15_51 |
| 669 |
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2))) |
| 670 |
|
| 671 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 672 |
# x <- dends_15_51 %>% untangle(method = "DendSer") |
| 673 |
x <- dends_15_51 %>% untangle(method = "ladderize") |
| 674 |
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2))) |
| 675 |
|
| 676 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 677 |
set.seed(3958) |
| 678 |
x <- dends_15_51 %>% untangle(method = "random", R = 10) |
| 679 |
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2))) |
| 680 |
|
| 681 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 682 |
x <- dends_15_51 %>% untangle(method = "step2side") |
| 683 |
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2))) |
| 684 |
|
| 685 |
## ----------------------------------------------------------------------------- |
| 686 |
set.seed(23235) |
| 687 |
ss <- sample(1:150, 10 ) |
| 688 |
dend1 <- iris[ss,-5] %>% dist %>% hclust("com") %>% as.dendrogram |
| 689 |
dend2 <- iris[ss,-5] %>% dist %>% hclust("single") %>% as.dendrogram |
| 690 |
dend3 <- iris[ss,-5] %>% dist %>% hclust("ave") %>% as.dendrogram |
| 691 |
dend4 <- iris[ss,-5] %>% dist %>% hclust("centroid") %>% as.dendrogram |
| 692 |
|
| 693 |
dend1234 <- dendlist("Complete" = dend1, "Single" = dend2, "Average" = dend3, "Centroid" = dend4) |
| 694 |
|
| 695 |
par(mfrow = c(2,2)) |
| 696 |
plot(dend1, main = "Complete") |
| 697 |
plot(dend2, main = "Single") |
| 698 |
plot(dend3, main = "Average") |
| 699 |
plot(dend4, main = "Centroid") |
| 700 |
|
| 701 |
|
| 702 |
## ----------------------------------------------------------------------------- |
| 703 |
all.equal(dend1, dend1) |
| 704 |
all.equal(dend1, dend2) |
| 705 |
all.equal(dend1, dend2, use.edge.length = FALSE) |
| 706 |
all.equal(dend1, dend2, use.edge.length = FALSE, use.topology = FALSE) |
| 707 |
|
| 708 |
all.equal(dend2, dend4, use.edge.length = TRUE) |
| 709 |
all.equal(dend2, dend4, use.edge.length = FALSE) |
| 710 |
|
| 711 |
all.equal(dendlist(dend1, dend1, dend1)) |
| 712 |
|
| 713 |
all.equal(dend1234) |
| 714 |
all.equal(dend1234, use.edge.length = FALSE) |
| 715 |
|
| 716 |
## ----------------------------------------------------------------------------- |
| 717 |
x <- 1:5 %>% dist %>% hclust %>% as.dendrogram |
| 718 |
y <- set(x, "labels", 5:1) |
| 719 |
|
| 720 |
dist.dendlist(dendlist(x1 = x,x2 = x,y1 = y)) |
| 721 |
dend_diff(x,y) |
| 722 |
|
| 723 |
dist.dendlist(dend1234) |
| 724 |
|
| 725 |
## ----------------------------------------------------------------------------- |
| 726 |
cor.dendlist(dend1234) |
| 727 |
|
| 728 |
## ----------------------------------------------------------------------------- |
| 729 |
library(corrplot) |
| 730 |
corrplot(cor.dendlist(dend1234), "pie", "lower") |
| 731 |
|
| 732 |
## ----fig.width=5, fig.height=3------------------------------------------------ |
| 733 |
# same subtrees, so there is no need to color the branches |
| 734 |
dend1234 %>% tanglegram(which = c(2,3)) |
| 735 |
# Here the branches colors are very helpful: |
| 736 |
dend1234 %>% tanglegram(which = c(1,2), |
| 737 |
common_subtrees_color_branches = TRUE) |
| 738 |
|
| 739 |
|
| 740 |
## ----------------------------------------------------------------------------- |
| 741 |
cor_bakers_gamma(dend15, dend51) |
| 742 |
|
| 743 |
## ----------------------------------------------------------------------------- |
| 744 |
cor_bakers_gamma(dend15, dend15) |
| 745 |
|
| 746 |
## ----------------------------------------------------------------------------- |
| 747 |
set.seed(23235) |
| 748 |
the_cor <- cor_bakers_gamma(dend15, dend15) |
| 749 |
the_cor2 <- cor_bakers_gamma(dend15, dend51) |
| 750 |
the_cor |
| 751 |
the_cor2 |
| 752 |
|
| 753 |
R <- 100 |
| 754 |
cor_bakers_gamma_results <- numeric(R) |
| 755 |
dend_mixed <- dend15 |
| 756 |
for(i in 1:R) { |
| 757 |
dend_mixed <- sample.dendrogram(dend_mixed, replace = FALSE) |
| 758 |
cor_bakers_gamma_results[i] <- cor_bakers_gamma(dend15, dend_mixed) |
| 759 |
} |
| 760 |
plot(density(cor_bakers_gamma_results), |
| 761 |
main = "Baker's gamma distribution under H0", |
| 762 |
xlim = c(-1,1)) |
| 763 |
abline(v = 0, lty = 2) |
| 764 |
abline(v = the_cor, lty = 2, col = 2) |
| 765 |
abline(v = the_cor2, lty = 2, col = 4) |
| 766 |
legend("topleft", legend = c("cor", "cor2"), fill = c(2,4)) |
| 767 |
round(sum(the_cor2 < cor_bakers_gamma_results)/ R, 4) |
| 768 |
title(sub = paste("One sided p-value:", |
| 769 |
"cor =", round(sum(the_cor < cor_bakers_gamma_results)/ R, 4), |
| 770 |
" ; cor2 =", round(sum(the_cor2 < cor_bakers_gamma_results)/ R, 4) |
| 771 |
)) |
| 772 |
|
| 773 |
## ----warning=FALSE------------------------------------------------------------ |
| 774 |
|
| 775 |
dend1 <- dend15 |
| 776 |
dend2 <- dend51 |
| 777 |
|
| 778 |
set.seed(23801) |
| 779 |
|
| 780 |
R <- 100 |
| 781 |
dend1_labels <- labels(dend1) |
| 782 |
dend2_labels <- labels(dend2) |
| 783 |
cor_bakers_gamma_results <- numeric(R) |
| 784 |
for(i in 1:R) { |
| 785 |
sampled_labels <- sample(dend1_labels, replace = TRUE) |
| 786 |
# members needs to be fixed since it will be later used in nleaves |
| 787 |
dend_mixed1 <- sample.dendrogram(dend1, |
| 788 |
dend_labels=dend1_labels, |
| 789 |
fix_members=TRUE,fix_order=TRUE,fix_midpoint=FALSE, |
| 790 |
replace = TRUE, sampled_labels=sampled_labels |
| 791 |
) |
| 792 |
dend_mixed2 <- sample.dendrogram(dend2, dend_labels=dend2_labels, |
| 793 |
fix_members=TRUE,fix_order=TRUE,fix_midpoint=FALSE, |
| 794 |
replace = TRUE, sampled_labels=sampled_labels |
| 795 |
) |
| 796 |
cor_bakers_gamma_results[i] <- cor_bakers_gamma(dend_mixed1, dend_mixed2, warn = FALSE) |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
# here is the tanglegram |
| 801 |
tanglegram(dend1, dend2) |
| 802 |
# And here is the tanglegram for one sample of our trees: |
| 803 |
dend_mixed1 <- rank_order.dendrogram(dend_mixed1) |
| 804 |
dend_mixed2 <- rank_order.dendrogram(dend_mixed2) |
| 805 |
dend_mixed1 <- fix_members_attr.dendrogram(dend_mixed1) |
| 806 |
dend_mixed2 <- fix_members_attr.dendrogram(dend_mixed2) |
| 807 |
tanglegram(dend_mixed1, dend_mixed2) |
| 808 |
cor_bakers_gamma(dend_mixed1, dend_mixed2, warn = FALSE) |
| 809 |
|
| 810 |
|
| 811 |
CI95 <- quantile(cor_bakers_gamma_results, probs=c(.025,.975)) |
| 812 |
CI95 |
| 813 |
par(mfrow = c(1,1)) |
| 814 |
plot(density(cor_bakers_gamma_results), |
| 815 |
main = "Baker's gamma bootstrap distribution", |
| 816 |
xlim = c(-1,1)) |
| 817 |
abline(v = CI95, lty = 2, col = 3) |
| 818 |
abline(v = cor_bakers_gamma(dend1, dend2), lty = 2, col = 2) |
| 819 |
legend("topleft", legend =c("95% CI", "Baker's Gamma Index"), fill = c(3,2)) |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
## ----------------------------------------------------------------------------- |
| 824 |
cor_cophenetic(dend15, dend51) |
| 825 |
|
| 826 |
## ----------------------------------------------------------------------------- |
| 827 |
hc1 <- hclust(dist(iris[,-5]), "com") |
| 828 |
hc2 <- hclust(dist(iris[,-5]), "single") |
| 829 |
|
| 830 |
# FM index of a cluster with himself is 1: |
| 831 |
FM_index(cutree(hc1, k=3), cutree(hc1, k=3)) |
| 832 |
# FM index of two clusterings: |
| 833 |
FM_index(cutree(hc1, k=3), cutree(hc2, k=3)) |
| 834 |
# we got a value far above the expected under H0 |
| 835 |
|
| 836 |
# Using the R code: |
| 837 |
FM_index_R(cutree(hc1, k=3), cutree(hc2, k=3)) |
| 838 |
|
| 839 |
## ----------------------------------------------------------------------------- |
| 840 |
FM_index(cutree(hc1, k=3), cutree(hc2, k=3)) |
| 841 |
|
| 842 |
## ----------------------------------------------------------------------------- |
| 843 |
0.4462 + 1.645 * sqrt(6.464092e-05) |
| 844 |
|
| 845 |
## ----------------------------------------------------------------------------- |
| 846 |
set.seed(23235) |
| 847 |
ss <- TRUE # sample(1:150, 30 ) # TRUE # |
| 848 |
hc1 <- hclust(dist(iris[ss,-5]), "com") |
| 849 |
hc2 <- hclust(dist(iris[ss,-5]), "single") |
| 850 |
dend1 <- as.dendrogram(hc1) |
| 851 |
dend2 <- as.dendrogram(hc2) |
| 852 |
# cutree(tree1) |
| 853 |
|
| 854 |
# It works the same for hclust and dendrograms: |
| 855 |
Bk(hc1, hc2, k = 3) |
| 856 |
Bk(dend1, dend2, k = 3) |
| 857 |
|
| 858 |
## ----warning=FALSE------------------------------------------------------------ |
| 859 |
Bk_plot(hc1, hc2, main = "WRONG Bk plot \n(due to the way cutree works with ties in hclust)", warn = FALSE) |
| 860 |
Bk_plot(dend1, dend2, main = "CORRECT Bk plot \n(based on dendrograms)") |
| 861 |
|
| 862 |
## ----cache=FALSE-------------------------------------------------------------- |
| 863 |
sessionInfo() |
| 864 |
|