| 1 |
## ----echo = FALSE, message = FALSE, warning=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 |
# dev = "svg", |
| 9 |
# comment = "#>", |
| 10 |
tidy = FALSE) |
| 11 |
|
| 12 |
# https://stackoverflow.com/questions/24091735/why-pandoc-does-not-retrieve-the-image-file |
| 13 |
# < ! -- rmarkdown v1 --> |
| 14 |
|
| 15 |
|
| 16 |
## ----------------------------------------------------------------------------- |
| 17 |
iris <- datasets::iris |
| 18 |
iris2 <- iris[,-5] |
| 19 |
species_labels <- iris[,5] |
| 20 |
library(colorspace) # get nice colors |
| 21 |
species_col <- rev(rainbow_hcl(3))[as.numeric(species_labels)] |
| 22 |
|
| 23 |
## ----fig.width=9, fig.height=9, fig.show='hold'------------------------------- |
| 24 |
# Plot a SPLOM: |
| 25 |
pairs(iris2, col = species_col, |
| 26 |
lower.panel = NULL, |
| 27 |
cex.labels=2, pch=19, cex = 1.2) |
| 28 |
|
| 29 |
# Add a legend |
| 30 |
par(xpd = TRUE) |
| 31 |
legend(x = 0.05, y = 0.4, cex = 2, |
| 32 |
legend = as.character(levels(species_labels)), |
| 33 |
fill = unique(species_col)) |
| 34 |
par(xpd = NA) |
| 35 |
|
| 36 |
|
| 37 |
## ----fig.height=3------------------------------------------------------------- |
| 38 |
# http://blog.safaribooksonline.com/2014/03/31/mastering-parallel-coordinate-charts-r/ |
| 39 |
par(las = 1, mar = c(4.5, 3, 3, 2) + 0.1, cex = .8) |
| 40 |
MASS::parcoord(iris2, col = species_col, var.label = TRUE, lwd = 2) |
| 41 |
|
| 42 |
# Add Title |
| 43 |
title("Parallel coordinates plot of the Iris data") |
| 44 |
# Add a legend |
| 45 |
par(xpd = TRUE) |
| 46 |
legend(x = 1.75, y = -.25, cex = 1, |
| 47 |
legend = as.character(levels(species_labels)), |
| 48 |
fill = unique(species_col), horiz = TRUE) |
| 49 |
par(xpd = NA) |
| 50 |
|
| 51 |
|
| 52 |
## ----fig.height = 10, fig.width=7--------------------------------------------- |
| 53 |
|
| 54 |
d_iris <- dist(iris2) # method="man" # is a bit better |
| 55 |
hc_iris <- hclust(d_iris, method = "complete") |
| 56 |
iris_species <- rev(levels(iris[,5])) |
| 57 |
|
| 58 |
library(dendextend) |
| 59 |
dend <- as.dendrogram(hc_iris) |
| 60 |
# order it the closest we can to the order of the observations: |
| 61 |
dend <- rotate(dend, 1:150) |
| 62 |
|
| 63 |
# Color the branches based on the clusters: |
| 64 |
dend <- color_branches(dend, k=3) #, groupLabels=iris_species) |
| 65 |
|
| 66 |
# Manually match the labels, as much as possible, to the real classification of the flowers: |
| 67 |
labels_colors(dend) <- |
| 68 |
rainbow_hcl(3)[sort_levels_values( |
| 69 |
as.numeric(iris[,5])[order.dendrogram(dend)] |
| 70 |
)] |
| 71 |
|
| 72 |
# We shall add the flower type to the labels: |
| 73 |
labels(dend) <- paste(as.character(iris[,5])[order.dendrogram(dend)], |
| 74 |
"(",labels(dend),")", |
| 75 |
sep = "") |
| 76 |
# We hang the dendrogram a bit: |
| 77 |
dend <- hang.dendrogram(dend,hang_height=0.1) |
| 78 |
# reduce the size of the labels: |
| 79 |
# dend <- assign_values_to_leaves_nodePar(dend, 0.5, "lab.cex") |
| 80 |
dend <- set(dend, "labels_cex", 0.5) |
| 81 |
# And plot: |
| 82 |
par(mar = c(3,3,3,7)) |
| 83 |
plot(dend, |
| 84 |
main = "Clustered Iris data set |
| 85 |
(the labels give the true flower species)", |
| 86 |
horiz = TRUE, nodePar = list(cex = .007)) |
| 87 |
legend("topleft", legend = iris_species, fill = rainbow_hcl(3)) |
| 88 |
|
| 89 |
#### BTW, notice that: |
| 90 |
# labels(hc_iris) # no labels, because "iris" has no row names |
| 91 |
# is.integer(labels(dend)) # this could cause problems... |
| 92 |
# is.character(labels(dend)) # labels are no longer "integer" |
| 93 |
|
| 94 |
## ----fig.width=7, fig.height=7------------------------------------------------ |
| 95 |
# Requires that the circlize package will be installed |
| 96 |
par(mar = rep(0,4)) |
| 97 |
circlize_dendrogram(dend) |
| 98 |
|
| 99 |
## ----echo=FALSE, eval=FALSE--------------------------------------------------- |
| 100 |
# # some_col_func <- function(n, top_color = "red4") { |
| 101 |
# # seq_cols <- c("#F7FCFD", "#E0ECF4", "#BFD3E6", "#9EBCDA", "#8C96C6", "#8C6BB1", |
| 102 |
# # "#88419D", "#810F7C") |
| 103 |
# # c(colorRampPalette(seq_cols, bias =1)(n-1), top_color) |
| 104 |
# # } |
| 105 |
# |
| 106 |
|
| 107 |
## ----fig.width=9, fig.height=9------------------------------------------------ |
| 108 |
|
| 109 |
some_col_func <- function(n) rev(colorspace::heat_hcl(n, c = c(80, 30), l = c(30, 90), power = c(1/5, 1.5))) |
| 110 |
|
| 111 |
# scaled_iris2 <- iris2 %>% as.matrix %>% scale |
| 112 |
# library(gplots) |
| 113 |
gplots::heatmap.2(as.matrix(iris2), |
| 114 |
main = "Heatmap for the Iris data set", |
| 115 |
srtCol = 20, |
| 116 |
dendrogram = "row", |
| 117 |
Rowv = dend, |
| 118 |
Colv = "NA", # this to make sure the columns are not ordered |
| 119 |
trace="none", |
| 120 |
margins =c(5,0.1), |
| 121 |
key.xlab = "Cm", |
| 122 |
denscol = "grey", |
| 123 |
density.info = "density", |
| 124 |
RowSideColors = rev(labels_colors(dend)), # to add nice colored strips |
| 125 |
col = some_col_func |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
## ----cache = FALSE, eval = FALSE---------------------------------------------- |
| 131 |
# heatmaply::heatmaply(as.matrix(iris2), |
| 132 |
# dendrogram = "row", |
| 133 |
# Rowv = dend) |
| 134 |
|
| 135 |
## ----------------------------------------------------------------------------- |
| 136 |
|
| 137 |
hclust_methods <- c("ward.D", "single", "complete", "average", "mcquitty", |
| 138 |
"median", "centroid", "ward.D2") |
| 139 |
iris_dendlist <- dendlist() |
| 140 |
for(i in seq_along(hclust_methods)) { |
| 141 |
hc_iris <- hclust(d_iris, method = hclust_methods[i]) |
| 142 |
iris_dendlist <- dendlist(iris_dendlist, as.dendrogram(hc_iris)) |
| 143 |
} |
| 144 |
names(iris_dendlist) <- hclust_methods |
| 145 |
iris_dendlist |
| 146 |
|
| 147 |
## ----fig.width=8, fig.height=8------------------------------------------------ |
| 148 |
iris_dendlist_cor <- cor.dendlist(iris_dendlist) |
| 149 |
iris_dendlist_cor |
| 150 |
corrplot::corrplot(iris_dendlist_cor, "pie", "lower") |
| 151 |
|
| 152 |
## ----fig.width=8, fig.height=8------------------------------------------------ |
| 153 |
iris_dendlist_cor_spearman <- cor.dendlist(iris_dendlist, method_coef = "spearman") |
| 154 |
corrplot::corrplot(iris_dendlist_cor_spearman, "pie", "lower") |
| 155 |
|
| 156 |
## ----fig.height=5------------------------------------------------------------- |
| 157 |
# The `which` parameter allows us to pick the elements in the list to compare |
| 158 |
iris_dendlist %>% dendlist(which = c(1,8)) %>% ladderize %>% |
| 159 |
set("branches_k_color", k=3) %>% |
| 160 |
# untangle(method = "step1side", k_seq = 3:20) %>% |
| 161 |
# set("clear_branches") %>% #otherwise the single lines are not black, since they retain the previous color from the branches_k_color. |
| 162 |
tanglegram(faster = TRUE) # (common_subtrees_color_branches = TRUE) |
| 163 |
|
| 164 |
## ----fig.height=5------------------------------------------------------------- |
| 165 |
# The `which` parameter allows us to pick the elements in the list to compare |
| 166 |
iris_dendlist %>% dendlist(which = c(1,4)) %>% ladderize %>% |
| 167 |
set("branches_k_color", k=2) %>% |
| 168 |
# untangle(method = "step1side", k_seq = 3:20) %>% |
| 169 |
tanglegram(faster = TRUE) # (common_subtrees_color_branches = TRUE) |
| 170 |
|
| 171 |
## ----fig.height=5------------------------------------------------------------- |
| 172 |
# The `which` parameter allows us to pick the elements in the list to compare |
| 173 |
iris_dendlist %>% dendlist(which = c(1,4)) %>% ladderize %>% |
| 174 |
# untangle(method = "step1side", k_seq = 3:20) %>% |
| 175 |
set("rank_branches") %>% |
| 176 |
tanglegram(common_subtrees_color_branches = TRUE) |
| 177 |
|
| 178 |
## ----------------------------------------------------------------------------- |
| 179 |
length(unique(common_subtrees_clusters(iris_dendlist[[1]], iris_dendlist[[4]]))[-1]) |
| 180 |
# -1 at the end is because we are ignoring the "0" subtree, which indicates leaves that are singletons. |
| 181 |
|
| 182 |
## ----fig.height=5------------------------------------------------------------- |
| 183 |
iris_dendlist %>% dendlist(which = c(3,4)) %>% ladderize %>% |
| 184 |
untangle(method = "step1side", k_seq = 2:6) %>% |
| 185 |
set("branches_k_color", k=2) %>% |
| 186 |
tanglegram(faster = TRUE) # (common_subtrees_color_branches = TRUE) |
| 187 |
|
| 188 |
## ----fig.height=15------------------------------------------------------------ |
| 189 |
par(mfrow = c(4,2)) |
| 190 |
for(i in 1:8) { |
| 191 |
iris_dendlist[[i]] %>% set("branches_k_color", k=2) %>% plot(axes = FALSE, horiz = TRUE) |
| 192 |
title(names(iris_dendlist)[i]) |
| 193 |
} |
| 194 |
|
| 195 |
## ----------------------------------------------------------------------------- |
| 196 |
iris_dendlist_cor2 <- cor.dendlist(iris_dendlist, method = "common") |
| 197 |
iris_dendlist_cor2 |
| 198 |
|
| 199 |
## ----fig.width=5, fig.height=5------------------------------------------------ |
| 200 |
# corrplot::corrplot(iris_dendlist_cor2, "pie", "lower") |
| 201 |
|
| 202 |
## ----------------------------------------------------------------------------- |
| 203 |
|
| 204 |
get_ordered_3_clusters <- function(dend) { |
| 205 |
cutree(dend, k = 3)[order.dendrogram(dend)] |
| 206 |
} |
| 207 |
|
| 208 |
dend_3_clusters <- lapply(iris_dendlist, get_ordered_3_clusters) |
| 209 |
|
| 210 |
compare_clusters_to_iris <- function(clus) {FM_index(clus, rep(1:3, each = 50), assume_sorted_vectors = TRUE)} |
| 211 |
|
| 212 |
clusters_performance <- sapply(dend_3_clusters, compare_clusters_to_iris) |
| 213 |
dotchart(sort(clusters_performance), xlim = c(0.7,1), |
| 214 |
xlab = "Fowlkes-Mallows Index (from 0 to 1)", |
| 215 |
main = "Perormance of clustering algorithms \n in detecting the 3 species", |
| 216 |
pch = 19) |
| 217 |
|
| 218 |
## ----------------------------------------------------------------------------- |
| 219 |
train <- dendextend::khan$train |
| 220 |
test <- dendextend::khan$test |
| 221 |
|
| 222 |
## ----------------------------------------------------------------------------- |
| 223 |
d_train <- train %>% dist %>% hclust %>% as.dendrogram |
| 224 |
d_test <- test %>% dist %>% hclust %>% as.dendrogram |
| 225 |
d_train_test <- dendlist(train = d_train, test = d_test) |
| 226 |
|
| 227 |
## ----------------------------------------------------------------------------- |
| 228 |
d_train_test %>% cor.dendlist |
| 229 |
|
| 230 |
## ----------------------------------------------------------------------------- |
| 231 |
d_train_test %>% cor.dendlist(method_coef = "spearman") |
| 232 |
|
| 233 |
## ----------------------------------------------------------------------------- |
| 234 |
Bk_plot(d_train, d_test, k = 2:30, xlim = c(2,30)) |
| 235 |
|
| 236 |
## ----fig.width=8, fig.height=5------------------------------------------------ |
| 237 |
pre_tang_d_train_test <- d_train_test %>% ladderize %>% # untangle %>% |
| 238 |
set("branches_k_color", k = 7) |
| 239 |
train_branches_colors <- get_leaves_branches_col(pre_tang_d_train_test$train) |
| 240 |
pre_tang_d_train_test %>% tanglegram(fast = TRUE, color_lines = train_branches_colors) |
| 241 |
|
| 242 |
## ----echo = FALSE------------------------------------------------------------- |
| 243 |
# dput(d_train_test_common) |
| 244 |
d_train_test_common <- structure(list(train = structure(list(structure(list(structure(171L, label = "491565", members = 1L, height = 0, leaf = TRUE), |
| 245 |
structure(178L, label = "505491", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 7.1369942952198), |
| 246 |
structure(list(structure(list(structure(8L, label = "283315", members = 1L, height = 0, leaf = TRUE), |
| 247 |
structure(9L, label = "897177", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 2.55936539399907), |
| 248 |
structure(list(structure(list(structure(106L, label = "345553", members = 1L, height = 0, leaf = TRUE), |
| 249 |
structure(112L, label = "307660", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 5.17910461856101), |
| 250 |
structure(list(structure(list(structure(268L, label = "504791", members = 1L, height = 0, leaf = TRUE), |
| 251 |
structure(306L, label = "782503", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 4.27052507661529), |
| 252 |
structure(list(structure(list(structure(246L, label = "81518", members = 1L, height = 0, leaf = TRUE), |
| 253 |
structure(290L, label = "280837", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 1.37572388944875), |
| 254 |
structure(list(structure(list(structure(266L, label = "866694", members = 1L, height = 0, leaf = TRUE), |
| 255 |
structure(277L, label = "811956", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 3.31301518861595), |
| 256 |
structure(list(structure(273L, label = "842918", members = 1L, height = 0, leaf = TRUE), |
| 257 |
structure(274L, label = "626555", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 2.71864544948399)), members = 4, midpoint = 1.5, height = 6.35097701381449)), members = 6, midpoint = 2, height = 8.7097033164167)), members = 8, midpoint = 2.25, height = 9.23807936424017)), members = 10, midpoint = 2.375, height = 11.6573350998416)), members = 12, midpoint = 2.4375, height = 17.5620766260713)), members = 14, midpoint = 2.46875, height = 30.2363452779928, class = "dendrogram"), |
| 258 |
test = structure(list(structure(list(structure(list(structure(171L, label = "491565", members = 1L, height = 0, leaf = TRUE), |
| 259 |
structure(178L, label = "505491", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 3.96666017450449), |
| 260 |
structure(list(structure(list(structure(list(structure(268L, label = "504791", members = 1L, height = 0, leaf = TRUE), |
| 261 |
structure(306L, label = "782503", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 2.31497882927685), |
| 262 |
structure(list(structure(list(structure(266L, label = "866694", members = 1L, height = 0, leaf = TRUE), |
| 263 |
structure(277L, label = "811956", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 1.75475236429532), |
| 264 |
structure(list(structure(273L, label = "842918", members = 1L, height = 0, leaf = TRUE), |
| 265 |
structure(274L, label = "626555", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 1.34617375921535)), members = 4, midpoint = 1.5, height = 2.76465021476497)), members = 6, midpoint = 2, height = 4.52927251774499), |
| 266 |
structure(list(structure(list(structure(246L, label = "81518", members = 1L, height = 0, leaf = TRUE), |
| 267 |
structure(290L, label = "280837", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 0.714433271901582), |
| 268 |
structure(list(structure(8L, label = "283315", members = 1L, height = 0, leaf = TRUE), |
| 269 |
structure(9L, label = "897177", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 1.71895552589356)), members = 4, midpoint = 1.5, height = 6.44143803354499)), members = 10, midpoint = 4.75, height = 7.736516720075)), members = 12, midpoint = 3.625, height = 11.0066972375913), |
| 270 |
structure(list(structure(106L, label = "345553", members = 1L, height = 0, leaf = TRUE), |
| 271 |
structure(112L, label = "307660", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 3.6486307417989)), members = 14, midpoint = 8.0625, height = 18.2331742971431, class = "dendrogram")), class = "dendlist", .Names = c("train", |
| 272 |
"test")) |
| 273 |
|
| 274 |
## ----------------------------------------------------------------------------- |
| 275 |
# This was calculated before |
| 276 |
# d_train_test_common <- d_train_test %>% prune_common_subtrees.dendlist |
| 277 |
# d_train_test_common |
| 278 |
d_train_test_common %>% untangle %>% tanglegram(common_subtrees_color_branches = TRUE) |
| 279 |
|
| 280 |
## ----------------------------------------------------------------------------- |
| 281 |
d_train_test %>% nleaves |
| 282 |
d_train_test_common %>% nleaves |
| 283 |
|
| 284 |
## ----------------------------------------------------------------------------- |
| 285 |
votes.repub <- cluster::votes.repub |
| 286 |
|
| 287 |
## ----fig.height=5------------------------------------------------------------- |
| 288 |
years <- as.numeric(gsub("X", "", colnames(votes.repub))) |
| 289 |
|
| 290 |
par(las = 2, mar = c(4.5, 3, 3, 2) + 0.1, cex = .8) |
| 291 |
# MASS::parcoord(votes.repub, var.label = FALSE, lwd = 1) |
| 292 |
matplot(1L:ncol(votes.repub), t(votes.repub), type = "l", col = 1, lty = 1, |
| 293 |
axes = F, xlab = "", ylab = "") |
| 294 |
axis(1, at = seq_along(years), labels = years) |
| 295 |
axis(2) |
| 296 |
# Add Title |
| 297 |
title("Votes for Republican Candidate\n in Presidential Elections \n (each line is a country - over the years)") |
| 298 |
|
| 299 |
## ----fig.width=9, fig.height=9------------------------------------------------ |
| 300 |
arcsin_transformation <- function(x) asin(x/100) |
| 301 |
|
| 302 |
dend_NA <- votes.repub %>% is.na %>% |
| 303 |
dist %>% hclust %>% as.dendrogram %>% ladderize |
| 304 |
|
| 305 |
dend <- votes.repub %>% arcsin_transformation %>% |
| 306 |
dist %>% hclust(method = "com") %>% as.dendrogram %>% |
| 307 |
rotate(labels(dend_NA)) %>% |
| 308 |
color_branches(k=3) |
| 309 |
|
| 310 |
# some_col_func <- function(n) rev(colorspace::heat_hcl(n, c = c(80, 30), l = c(30, 90), power = c(1/5, 1.5))) |
| 311 |
some_col_func <- colorspace::diverge_hcl |
| 312 |
|
| 313 |
|
| 314 |
# par(mar = c(3,3,3,3)) |
| 315 |
# library(gplots) |
| 316 |
gplots::heatmap.2(as.matrix(votes.repub), |
| 317 |
main = "Votes for\n Republican Presidential Candidate\n (clustered using complete)", |
| 318 |
srtCol = 60, |
| 319 |
dendrogram = "row", |
| 320 |
Rowv = dend, |
| 321 |
Colv = "NA", # this to make sure the columns are not ordered |
| 322 |
trace="none", |
| 323 |
margins =c(3,6), |
| 324 |
key.xlab = "% Votes for Republican\n Presidential Candidate", |
| 325 |
labCol = years, |
| 326 |
denscol = "grey", |
| 327 |
density.info = "density", |
| 328 |
col = some_col_func |
| 329 |
) |
| 330 |
# RowSideColors = rev(labels_colors(dend)), # to add nice colored strips |
| 331 |
|
| 332 |
## ----------------------------------------------------------------------------- |
| 333 |
|
| 334 |
hclust_methods <- c("ward.D", "single", "complete", "average", "mcquitty", |
| 335 |
"median", "centroid", "ward.D2") |
| 336 |
votes.repub_dendlist <- dendlist() |
| 337 |
|
| 338 |
for(i in seq_along(hclust_methods)) { |
| 339 |
tmp_dend <- votes.repub %>% arcsin_transformation %>% dist %>% hclust(method = hclust_methods[i]) %>% as.dendrogram |
| 340 |
votes.repub_dendlist <- dendlist(votes.repub_dendlist, tmp_dend) |
| 341 |
} |
| 342 |
names(votes.repub_dendlist) <- hclust_methods |
| 343 |
# votes.repub_dendlist |
| 344 |
|
| 345 |
## ----fig.width=8, fig.height=8------------------------------------------------ |
| 346 |
corrplot::corrplot(cor.dendlist(votes.repub_dendlist), "pie", "lower") |
| 347 |
|
| 348 |
## ----echo=FALSE, fig.width=9, fig.height=9------------------------------------ |
| 349 |
arcsin_transformation <- function(x) asin(x/100) |
| 350 |
|
| 351 |
dend_NA <- votes.repub %>% is.na %>% |
| 352 |
dist %>% hclust %>% as.dendrogram %>% ladderize |
| 353 |
|
| 354 |
dend <- votes.repub %>% arcsin_transformation %>% |
| 355 |
dist %>% hclust(method = "ave") %>% as.dendrogram %>% |
| 356 |
rotate(labels(dend_NA)) %>% |
| 357 |
color_branches(k=3) |
| 358 |
|
| 359 |
# some_col_func <- function(n) rev(colorspace::heat_hcl(n, c = c(80, 30), l = c(30, 90), power = c(1/5, 1.5))) |
| 360 |
some_col_func <- colorspace::diverge_hcl |
| 361 |
|
| 362 |
|
| 363 |
# par(mar = c(3,3,3,3)) |
| 364 |
# library(gplots) |
| 365 |
gplots::heatmap.2(as.matrix(votes.repub), |
| 366 |
main = "Votes for\n Republican Presidential Candidate\n (clustered using average)", |
| 367 |
srtCol = 60, |
| 368 |
dendrogram = "row", |
| 369 |
Rowv = dend, |
| 370 |
Colv = "NA", # this to make sure the columns are not ordered |
| 371 |
trace="none", |
| 372 |
margins =c(3,6), |
| 373 |
key.xlab = "% Votes for Republican\n Presidential Candidate", |
| 374 |
labCol = years, |
| 375 |
denscol = "grey", |
| 376 |
density.info = "density", |
| 377 |
col = some_col_func |
| 378 |
) |
| 379 |
# RowSideColors = rev(labels_colors(dend)), # to add nice colored strips |
| 380 |
|
| 381 |
## ----echo=FALSE--------------------------------------------------------------- |
| 382 |
|
| 383 |
ord1 <- c("North Carolina", "Virginia", "Tennessee", "Kentucky", "Maryland", |
| 384 |
"Delaware", "Oklahoma", "Missouri", "New Mexico", "Oregon", "Washington", |
| 385 |
"California", "West Virginia", "Hawaii", "Nevada", "Arizona", |
| 386 |
"Montana", "Idaho", "Wyoming", "Utah", "Colorado", "Alaska", |
| 387 |
"Illinois", "New York", "Indiana", "Ohio", "Connecticut", "New Hampshire", |
| 388 |
"New Jersey", "Pennsylvania", "Iowa", "South Dakota", "North Dakota", |
| 389 |
"Wisconsin", "Minnesota", "Nebraska", "Kansas", "Maine", "Michigan", |
| 390 |
"Massachusetts", "Rhode Island", "Vermont", "Alabama", "Georgia", |
| 391 |
"Louisiana", "Arkansas", "Florida", "Texas", "South Carolina", |
| 392 |
"Mississippi") |
| 393 |
|
| 394 |
ord2 <- c("North Carolina", "Virginia", "Tennessee", "Oklahoma", "Kentucky", |
| 395 |
"Maryland", "Delaware", "Missouri", "New Mexico", "West Virginia", |
| 396 |
"Oregon", "Washington", "California", "Nevada", "Arizona", "Montana", |
| 397 |
"Colorado", "Alaska", "Idaho", "Wyoming", "Utah", "Hawaii", "Maine", |
| 398 |
"Illinois", "New York", "New Jersey", "Indiana", "Ohio", "Connecticut", |
| 399 |
"New Hampshire", "Pennsylvania", "Michigan", "Iowa", "South Dakota", |
| 400 |
"North Dakota", "Wisconsin", "Minnesota", "Massachusetts", "Rhode Island", |
| 401 |
"Nebraska", "Kansas", "Vermont", "Alabama", "Georgia", "Louisiana", |
| 402 |
"Arkansas", "Florida", "Texas", "South Carolina", "Mississippi" |
| 403 |
) |
| 404 |
|
| 405 |
# dput(lapply(dends, labels)[[2]]) |
| 406 |
|
| 407 |
|
| 408 |
## ----------------------------------------------------------------------------- |
| 409 |
dend_com <- votes.repub %>% arcsin_transformation %>% |
| 410 |
dist %>% hclust(method = "com") %>% as.dendrogram %>% |
| 411 |
rotate(labels(dend_NA)) %>% |
| 412 |
color_branches(k=3) # %>% ladderize |
| 413 |
dend_ave <- votes.repub %>% arcsin_transformation %>% |
| 414 |
dist %>% hclust(method = "ave") %>% as.dendrogram %>% |
| 415 |
rotate(labels(dend_NA)) %>% |
| 416 |
color_branches(k=3) # %>% ladderize |
| 417 |
|
| 418 |
# The orders were predefined after using untangle("step2side") |
| 419 |
# They are omitted here to save running time. |
| 420 |
dend_com <- rotate(dend_com, ord1) |
| 421 |
dend_ave <- rotate(dend_ave, ord2) |
| 422 |
|
| 423 |
dends <- dendlist(complete = dend_com, average = dend_ave) # %>% untangle("step2side") |
| 424 |
dends %>% tanglegram(margin_inner = 7) |
| 425 |
|
| 426 |
|
| 427 |
## ----------------------------------------------------------------------------- |
| 428 |
animals <- cluster::animals |
| 429 |
|
| 430 |
colnames(animals) <- c("warm-blooded", |
| 431 |
"can fly", |
| 432 |
"vertebrate", |
| 433 |
"endangered", |
| 434 |
"live in groups", |
| 435 |
"have hair") |
| 436 |
|
| 437 |
## ----fig.width=9, fig.height=9------------------------------------------------ |
| 438 |
|
| 439 |
dend_r <- animals %>% dist(method = "man") %>% hclust(method = "ward.D") %>% as.dendrogram %>% ladderize %>% |
| 440 |
color_branches(k=4) |
| 441 |
|
| 442 |
dend_c <- t(animals) %>% dist(method = "man") %>% hclust(method = "com") %>% as.dendrogram %>% ladderize%>% |
| 443 |
color_branches(k=3) |
| 444 |
|
| 445 |
|
| 446 |
# some_col_func <- function(n) rev(colorspace::heat_hcl(n, c = c(80, 30), l = c(30, 90), power = c(1/5, 1.5))) |
| 447 |
# some_col_func <- colorspace::diverge_hcl |
| 448 |
# some_col_func <- colorspace::sequential_hcl |
| 449 |
some_col_func <- function(n) (colorspace::diverge_hcl(n, h = c(246, 40), c = 96, l = c(65, 90))) |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
# par(mar = c(3,3,3,3)) |
| 454 |
# library(gplots) |
| 455 |
gplots::heatmap.2(as.matrix(animals-1), |
| 456 |
main = "Attributes of Animals", |
| 457 |
srtCol = 35, |
| 458 |
Rowv = dend_r, |
| 459 |
Colv = dend_c, |
| 460 |
trace="row", hline = NA, tracecol = "darkgrey", |
| 461 |
margins =c(6,3), |
| 462 |
key.xlab = "no / yes", |
| 463 |
denscol = "grey", |
| 464 |
density.info = "density", |
| 465 |
col = some_col_func |
| 466 |
) |
| 467 |
|
| 468 |
|
| 469 |
## ----------------------------------------------------------------------------- |
| 470 |
|
| 471 |
hclust_methods <- c("ward.D", "single", "complete", "average", "mcquitty", |
| 472 |
"median", "centroid", "ward.D2") |
| 473 |
animals_dendlist <- dendlist() |
| 474 |
|
| 475 |
for(i in seq_along(hclust_methods)) { |
| 476 |
tmp_dend <- animals %>% dist(method = "man") %>% |
| 477 |
hclust(method = hclust_methods[i]) %>% as.dendrogram |
| 478 |
animals_dendlist <- dendlist(animals_dendlist, tmp_dend) |
| 479 |
} |
| 480 |
names(animals_dendlist) <- hclust_methods |
| 481 |
# votes.repub_dendlist |
| 482 |
|
| 483 |
## ----fig.width=8, fig.height=8------------------------------------------------ |
| 484 |
cophenetic_cors <- cor.dendlist(animals_dendlist) |
| 485 |
corrplot::corrplot(cophenetic_cors, "pie", "lower") |
| 486 |
|
| 487 |
## ----------------------------------------------------------------------------- |
| 488 |
remove_median <- dendlist(animals_dendlist, which = c(1:8)[-6] ) |
| 489 |
FM_cors <- cor.dendlist(remove_median, method = "FM_index", k = 4) |
| 490 |
corrplot::corrplot(FM_cors, "pie", "lower") |
| 491 |
|