forked from YuLab-SMU/ggtreeDendro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples.R
170 lines (110 loc) · 3.65 KB
/
Examples.R
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
163
164
165
166
167
168
169
170
## `hclust` and `dendrogram` objects
# genieclust::gclust() also output `hclust` object
# amap::hcluster() also output `hclust` object
# FactoClass::ward.cluster() also output `hclust` object
d <- dist(USArrests)
hc <- hclust(d, "ave")
den <- as.dendrogram(hc)
p1 <- autoplot(hc) + geom_tiplab()
p2 <- autoplot(den) + geom_rect_subtree(4)
plot_list(p1, p2, ncol=2)
## `linkage` object
library("mdendro")
lnk <- linkage(d, digits = 1, method = "complete")
autoplot(lnk, layout = 'circular') + geom_tiplab() +
scale_color_subtree(4) + theme_tree()
## `agnes`, `diana` and `twins` objects
library(cluster)
x1 <- agnes(mtcars)
x2 <- diana(mtcars)
p1 <- autoplot(x1) + geom_tiplab()
p2 <- autoplot(x2) + geom_tiplab()
plot_list(p1, p2, ncol=2)
## `pvclust` object
library(pvclust)
data(Boston, package = "MASS")
set.seed(123)
result <- pvclust(Boston, method.dist="cor", method.hclust="average", nboot=1000, parallel=TRUE)
autoplot(result, label_edge=TRUE, pvrect = TRUE) + geom_tiplab()
## `bclust` object
### S4
library(flexclust)
data(iris)
bc1 <- bclust(iris[,1:4], 3, base.k=5)
autoplot(bc1)
### S3
library(e1071)
bc2 <- bclust(iris[,1:4], 3, base.centers=5)
autoplot(bc2)
## `protoclust` object
library(protoclust)
data(Boston, package = "MASS")
d <- dist(Boston)
pc <- protoclust(d)
plotwithprototypes(pc)
autoplot(pc)
## `hdbscan` object
library(dbscan)
res <- hdbscan(moons, minPts = 5)
autoplot(res)
## `hkmeans` object
library(factoextra)
# Load data
data(USArrests)
# Scale the data
df <- scale(USArrests)
# Compute hierarchical k-means clustering
res.hk <-hkmeans(df, 4)
hkmeans_tree(res.hk, cex = 0.6)
fviz_dend(res.hk, cex = 0.6)
library(ggtreeDendro)
autoplot(res.hk) + geom_rect_subtree(4, color=c("red", "blue", "green", "purple"))
autoplot(res.hk$hclust) + scale_color_subtree(4)
# ClusterExperiment
library(clusterExperiment)
data(simData)
#create a clustering, for 8 clusters (truth was 3)
cl <-clusterSingle(simData, subsample=FALSE,
sequential=FALSE,
mainClusterArgs=list(clusterFunction="pam", clusterArgs=list(k=8)))
#create dendrogram of clusters and then
# merge clusters based ondendrogram:
cl <- makeDendrogram(cl)
cl <- mergeClusters(cl,mergeMethod="adjP",DEMethod="limma",
cutoff=0.1,plot=FALSE)
plotDendrogram(cl,leafType="samples",whichClusters="all",plotType="colorblock")
library(ggtreeDendro)
autoplot(cl)
## dendro
library(ggplot2)
library(ggtree)
library(ggdendro)
library(ggtreeDendro)
hc <- hclust(dist(USArrests), "ave")
x <- dendro_data(hc)
autoplot(x) + geom_tiplab()
## HGC
library(HGC)
data(Pollen)
Pollen.PCs <- Pollen[["PCs"]]
Pollen.Label.Tissue <- Pollen[["Tissue"]]
Pollen.Label.CellLine <- Pollen[["CellLine"]]
Pollen.SNN <- SNN.Construction(Pollen.PCs)
rownames(Pollen.SNN) <- rownames(Pollen.PCs)
Pollen.ClusteringTree <- HGC.dendrogram(G = Pollen.SNN)
Pollen.labels <- data.frame(Tissue = Pollen.Label.Tissue,
CellLine = Pollen.Label.CellLine)
HGC.PlotDendrogram(tree = Pollen.ClusteringTree,
k = 5, plot.label = TRUE,
labels = Pollen.labels)
##
library(treeio)
library(ggtree)
library(ggtreeDendro)
x <- as.phylo(as.dendrogram(Pollen.ClusteringTree))
p1 <- ggtree(x, layout = 'dendrogram', ladderize = FALSE)
p <- ggtree(x, layout = 'dendrogram', ladderize = FALSE, branch.length = 'none')
g <- gheatmap(p, Pollen.labels[,1, drop=FALSE], width=.05, colnames_angle = 0, colnames_offset_y = -20, color=NA)
g <- g + ggnewscale::new_scale_fill()
g <- gheatmap(g, Pollen.labels[,2, drop=FALSE], width=.05, colnames_angle = 0, colnames_offset_y = -20, color=NA, offset = 1)
g + scale_color_subtree(5)