if (FALSE) {
set.seed(23235)
ss <- sample(1:150, 10)
# Getting the dend object
dend <- iris[ss, -5] %>%
dist() %>%
hclust() %>%
as.dendrogram()
dend %>% plot()
dend %>% labels()
dend %>%
set("labels", 1:10) %>%
labels()
dend %>%
set("labels", 1:10) %>%
plot()
dend %>%
set("labels_color") %>%
plot()
dend %>%
set("labels_col", c(1, 2)) %>%
plot() # Works also with partial matching :)
dend %>%
set("labels_cex", c(1, 1.2)) %>%
plot()
dend %>%
set("leaves_pch", NA) %>%
plot()
dend %>%
set("leaves_pch", c(1:5)) %>%
plot()
dend %>%
set("leaves_pch", c(19, 19, NA)) %>%
set("leaves_cex", c(1, 2)) %>%
plot()
dend %>%
set("leaves_pch", c(19, 19, NA)) %>%
set("leaves_cex", c(1, 2)) %>%
set("leaves_col", c(1, 1, 2, 2)) %>%
plot()
dend %>%
set("hang") %>%
plot()
# using bg for leaves and nodes
set.seed(23235)
ss <- sample(1:150, 25)
# Getting the dend object
dend25 <- iris[ss, -5] %>%
dist() %>%
hclust() %>%
as.dendrogram()
dend25 %>%
set("labels", 1:25) %>%
set("nodes_pch", 21) %>% # set all nodes to be pch 21
set("nodes_col", "darkred") %>%
set("nodes_bg", "gold") %>%
set("leaves_pch", 1:25) %>% # Change the leaves pch to move from 1 to 25
set("leaves_col", "darkred") %>%
set("leaves_bg", "gold") %>%
plot(main = "pch 21 to 25 supports the\nnodes_bg and leaves_bg parameters")
dend %>%
set("branches_k_col") %>%
plot()
dend %>%
set("branches_k_col", c(1, 2)) %>%
plot()
dend %>%
set("branches_k_col", c(1, 2, 3), k = 3) %>%
plot()
dend %>%
set("branches_k_col", k = 3) %>%
plot()
dend %>%
set("branches_k_lty", k = 3) %>%
plot()
dend %>%
set("branches_k_col", k = 3) %>%
set("branches_k_lty", k = 3) %>%
plot()
dend %>%
set("branches_col", c(1, 2, 1, 2, NA)) %>%
plot()
dend %>%
set("branches_lwd", c(2, 1, 2)) %>%
plot()
dend %>%
set("branches_lty", c(1, 2, 1)) %>%
plot()
# clears all of the things added to the leaves
dend %>%
set("labels_color", c(19, 19, NA)) %>%
set("leaves_pch", c(19, 19, NA)) %>% # plot
set("clear_leaves") %>% # remove all of what was done until this point
plot()
# Different order
dend %>%
set("leaves_pch", c(19, 19, NA)) %>%
set("labels_color", c(19, 19, NA)) %>%
set("clear_leaves") %>%
plot()
# doing this without chaining (%>%) will NOT be fun:
dend %>%
set("labels", 1:10) %>%
set("labels_color") %>%
set("branches_col", c(1, 2, 1, 2, NA)) %>%
set("branches_lwd", c(2, 1, 2)) %>%
set("branches_lty", c(1, 2, 1)) %>%
set("hang") %>%
plot()
par(mfrow = c(1, 3))
dend %>%
set("highlight_branches_col") %>%
plot()
dend %>%
set("highlight_branches_lwd") %>%
plot()
dend %>%
set("highlight_branches_col") %>%
set("highlight_branches_lwd") %>%
plot()
par(mfrow = c(1, 1))
#----------------------------
# Examples for: by_labels_branches_col, by_labels_branches_lwd, by_labels_branches_lty
old_labels <- labels(dend)
dend %>%
set("labels", seq_len(nleaves(dend))) %>%
set("by_labels_branches_col", c(1:4, 7)) %>%
set("by_labels_branches_lwd", c(1:4, 7)) %>%
set("by_labels_branches_lty", c(1:4, 7)) %>%
set("labels", old_labels) %>%
plot()
dend %>%
set("labels", seq_len(nleaves(dend))) %>%
set("by_labels_branches_col", c(1:4, 7), type = "any", TF_values = c(4, 2)) %>%
set("by_labels_branches_lwd", c(1:4, 7), type = "all", TF_values = c(4, 1)) %>%
set("by_labels_branches_lty", c(1:4, 7), TF_values = c(4, 1)) %>%
plot()
#---- using order_value
# This is probably not what you want, since cutree
# returns clusters in the order of the original data:
dend %>%
set("labels_colors", cutree(dend, k = 3)) %>%
plot()
# The way to fix it, is to use order_value = TRUE
# so that value is assumed to be in the order of the data:
dend %>%
set("labels_colors", cutree(dend, k = 3), order_value = TRUE) %>%
plot()
#----------------------------
# Example for: by_lists_branches_col, by_lists_branches_lwd, by_lists_branches_lty
L <- list(c("109", "123", "126", "145"), "29", c("59", "67", "97"))
dend %>%
set("by_lists_branches_col", L, TF_value = "blue") %>%
set("by_lists_branches_lwd", L, TF_value = 4) %>%
set("by_lists_branches_lty", L, TF_value = 3) %>%
plot()
#----------------------------
# A few dendlist examples:
dendlist(dend, dend) %>%
set("hang") %>%
plot()
dendlist(dend, dend) %>%
set("branches_k_col", k = 3) %>%
plot()
dendlist(dend, dend) %>%
set("labels_col", c(1, 2)) %>%
plot()
dendlist(dend, dend) %>%
set("hang") %>%
set("labels_col", c(1, 2), which = 1) %>%
set("branches_k_col", k = 3, which = 2) %>%
set("labels_cex", 1.2) %>%
plot()
#----------------------------
# example of modifying the dendrogram in a heatmap:
library(gplots)
data(mtcars)
x <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
##
##' demonstrate the effect of row and column dendrogram options
##
Rowv_dend <- x %>%
dist() %>%
hclust() %>%
as.dendrogram() %>%
set("branches_k", k = 3) %>%
set("branches_lwd", 2) %>%
ladderize() # rotate_DendSer
Colv_dend <- t(x) %>%
dist() %>%
hclust() %>%
as.dendrogram() %>%
set("branches_k", k = 3) %>%
set("branches_lwd", 2) %>%
ladderize() # rotate_DendSer
heatmap.2(x, Rowv = Rowv_dend, Colv = Colv_dend)
}
Run the code above in your browser using DataLab