-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDendrogram Visualizations.Rmd
163 lines (109 loc) · 3.75 KB
/
Dendrogram Visualizations.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: "R Notebook"
output:
pdf_document: default
html_notebook: default
---
This is for how to create great visualizations in R for clstering
```{r}
#Loading Data
data("USArrests")
#compute dissimilarity matrix
dd <- dist(scale(USArrests), method = "euclidean")
#Hierarchial Clustering
hc <- hclust(dd, method = "ward.D2")
#hang put labels at same height
plot(hc, hang = -1, cex = 0.6)
```
```{r}
#Dendrogram
hcd <- as.dendrogram(hc)
plot(hcd, type = "rectangle", ylab = "Height")
plot(hcd, type = "triangle", ylab = "Height")
#Customizing hcd plot
nodepar <- list(lab.cex = 0.6, pch = c(NA, 19), cex = 0.7, col = "blue")
plot(hcd, ylab = "Height", nodePar = nodepar, leaflab = "none", edgePar = list(col = 2:3, lwd = 2:1))
#if we write horiz = TRUW then plot will be horizontal
#edgepar to change color of branches
```
```{r}
#Phylogenetic Trees
library(ape)
plot(as.phylo(hc), cex = 0.6, type = "fan", label.offset = 0.5)
#Type can be cladogram, unrooted, fan, radial or phylogram : default is phylogram
#cut dendrogram into 4 clusters
colors = c("red", "blue", "green", "black")
clus4 = cutree(hc,4)
plot(as.phylo(hc), type = "fan", tip.color = colors[clus4], label.offset = 1, cex = 0.7)
plot(as.phylo(hc), type = "cladogram", cex = 0.6,
edge.color = "steelblue", edge.width = 2, edge.lty = 2,
tip.color = "steelblue")
```
```{r}
library(ggdendro)
library(ggplot2)
ggdendrogram(hc, theme_dendro = FALSE)
dend <- as.dendrogram(hc)
dend_data <- dendro_data(dend, type = "rectangle")
names(dend_data)
ggplot(dend_data$segments) + geom_segment(aes(x=x,y=y,xend=xend,yend=yend)) +
geom_text(data = dend_data$labels, aes(x,y,label = label), hjust=1, angle=90, size =3) + ylim(-3,15)
```
```{r}
#Dendextend
library(dendextend)
dend <- USArrests[1:5,] %>% # data
scale %>% # Scale the data
dist %>% # calculate a distance matrix,
hclust(method = "ward.D2") %>% # Hierarchical clustering
as.dendrogram # Turn the object into a dendrogram.
plot(dend)
dend %>% set("labels_col", value = c("green", "blue"), k=2) %>%
plot(main = "Color labels \nper cluster")
abline(h = 2, lty = 2)
dend %>% set("nodes_pch", 19) %>% # node point type
set("nodes_cex", 2) %>% # node point size
set("nodes_col", "blue") %>% # node point color
plot(main = "Node points")
dend %>% set("leaves_pch", 19) %>% # node point type
set("leaves_cex", 2) %>% # node point size
set("leaves_col", "blue") %>% # node point color
plot(main = "Leaves points")
dend %>% set("leaves_pch", c(17, 18, 19)) %>% # node point type
set("leaves_cex", 2) %>% # node point size
set("leaves_col", c("blue", "red", "green")) %>% #node point color
plot(main = "Leaves points")
dend %>% set("branches_k_color",
value = c("red", "blue"), k = 2) %>%
plot(main = "Customized colors")
dend %>% set("branches_k_color", k = 3) %>% plot
dend %>% rect.dendrogram(k=3, border = 8, lty = 5, lwd = 2)
dend <- iris[1:30,-5] %>% scale %>% dist %>%
hclust %>% as.dendrogram %>%
set("branches_k_color", k=3) %>% set("branches_lwd", 1.2) %>%
set("labels_colors") %>% set("labels_cex", c(.9,1.2)) %>%
set("leaves_pch", 19) %>% set("leaves_col", c("blue", "red"))
# plot the dend in usual "base" plotting engine:
plot(dend)
ggd1 <- as.ggdend(dend)
ggplot(ggd1, theme = theme_minimal())
#Radial plot
ggplot(ggd1, labels = TRUE) +
scale_y_reverse(expand = c(0.2, 0)) +
coord_polar(theta="x")
```
```{r}
#pvclust
library(pvclust)
data(lung)
set.seed(1234)
result <- pvclust(lung[1:100, 1:10], method.dist="cor",
method.hclust="average", nboot=10)
plot(result)
pvrect(result)
result %>% as.dendrogram %>%
set("branches_k_color", k = 2, value = c("purple", "orange")) %>%
plot
result %>% text
result %>% pvrect
```