View source: R/many.clusters_V.R
1 | many.clusters_V(x, resultsDir, Filename, Title, parameters, toPDF = TRUE, toPNG=TRUE, conditions = NULL, colors = NULL)
|
x |
|
resultsDir |
|
Filename |
|
Title |
|
parameters |
|
toPDF |
|
toPNG |
|
conditions |
|
colors |
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 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (x, resultsDir, Filename, Title, parameters, toPDF = TRUE,
conditions = NULL, colors = NULL)
{
labels <- colnames(x)
use.cor = "pairwise.complete.obs"
if (toPDF) {
pdf(file = file.path(resultsDir, paste(Filename, "pdf",
sep = ".")))
}
if (is.null(conditions)) {
opt <- par(cex.main = 2, cex = parameters$ce, cex.lab = 1.5,
cex.axis = 1.5)
clust.cor.ward <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "ward.D2")
plot(clust.cor.ward, main = Title, hang = -1)
clust.cor.average <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "average")
plot(clust.cor.average, main = Title, hang = -1)
clust.cor.complete <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "complete")
plot(clust.cor.complete, main = Title, hang = -1)
clust.euclid.ward <- hclust(dist(t(x)), method = "ward.D2")
plot(clust.euclid.ward, main = Title, hang = -1)
clust.euclid.average <- hclust(dist(t(x)), method = "average")
plot(clust.euclid.average, main = Title, hang = -1)
clust.euclid.complete <- hclust(dist(t(x)), method = "complete")
plot(clust.euclid.complete, main = Title, hang = -1)
par(opt)
}
else if (is.null(colors)) {
list1 <- unique(as.character(sort(conditions)))
ColVect <- c(brewer.pal(8, "Dark2"), brewer.pal(12, "Paired"))
list2 <- ColVect[1:length(unique(conditions))]
map = setNames(list2, list1)
colors <- map[conditions]
color_cluster <- function(hclus, condition, ce) {
sampleDendrogram <- as.dendrogram(hclus)
names(condition) <- hclus$labels
sampleDendrogram <- dendrapply(sampleDendrogram,
function(x, batch) {
if (is.leaf(x)) {
label <- attr(x, "label")
attr(x, "nodePar") <- list(lab.col = as.vector(batch[label]),
pch = "", cex = 0.8, lab.cex = ce)
}
x
}, condition)
}
opt <- par(cex.main = 1, cex.axis = 0.8, cex = 0.8)
clust.cor.ward <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "ward.D2")
clust.cor.ward <- color_cluster(clust.cor.ward, colors,
parameters$ce)
plot(clust.cor.ward, main = Title, xlab = "Ward")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.cor.average <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "average")
clust.cor.average <- color_cluster(clust.cor.average,
colors, parameters$ce)
plot(clust.cor.average, main = Title, xlab = "Average")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.cor.complete <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "complete")
clust.cor.complete <- color_cluster(clust.cor.complete,
colors, parameters$ce)
plot(clust.cor.complete, main = Title, xlab = "Complete")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.euclid.ward <- hclust(dist(t(x)), method = "ward.D2")
clust.euclid.ward <- color_cluster(clust.euclid.ward,
colors, parameters$ce)
plot(clust.euclid.ward, main = Title, xlab = "Euclidean Ward")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.euclid.average <- hclust(dist(t(x)), method = "average")
clust.euclid.average <- color_cluster(clust.euclid.average,
colors, parameters$ce)
plot(clust.euclid.average, main = Title, xlab = "Euclidean Average")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.euclid.complete <- hclust(dist(t(x)), method = "complete")
clust.euclid.complete <- color_cluster(clust.euclid.complete,
colors, parameters$ce)
plot(clust.euclid.complete, main = Title, xlab = "Euclidean Complete")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
par(opt)
}
else {
list1 <- unique(as.character(conditions))
list2 <- unique(colors)
map = setNames(list2, list1)
colors <- map[conditions]
color_cluster <- function(hclus, condition, ce) {
sampleDendrogram <- as.dendrogram(hclus)
names(condition) <- hclus$labels
sampleDendrogram <- dendrapply(sampleDendrogram,
function(x, batch) {
if (is.leaf(x)) {
label <- attr(x, "label")
attr(x, "nodePar") <- list(lab.col = as.vector(batch[label]),
pch = "", cex = 0.8, lab.cex = ce)
}
x
}, condition)
}
opt <- par(cex.main = 1, cex.axis = 0.8, cex = 0.8)
clust.cor.ward <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "ward.D2")
clust.cor.ward <- color_cluster(clust.cor.ward, colors,
parameters$ce)
plot(clust.cor.ward, main = Title, xlab = "Ward")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.cor.average <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "average")
clust.cor.average <- color_cluster(clust.cor.average,
colors, parameters$ce)
plot(clust.cor.average, main = Title, xlab = "Average")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.cor.complete <- hclust(as.dist(1 - cor(x, use = use.cor)),
method = "complete")
clust.cor.complete <- color_cluster(clust.cor.complete,
colors, parameters$ce)
plot(clust.cor.complete, main = Title, xlab = "Complete")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.euclid.ward <- hclust(dist(t(x)), method = "ward.D2")
clust.euclid.ward <- color_cluster(clust.euclid.ward,
colors, parameters$ce)
plot(clust.euclid.ward, main = Title, xlab = "Euclidean Ward")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.euclid.average <- hclust(dist(t(x)), method = "average")
clust.euclid.average <- color_cluster(clust.euclid.average,
colors, parameters$ce)
plot(clust.euclid.average, main = Title, xlab = "Euclidean Average")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
clust.euclid.complete <- hclust(dist(t(x)), method = "complete")
clust.euclid.complete <- color_cluster(clust.euclid.complete,
colors, parameters$ce)
plot(clust.euclid.complete, main = Title, xlab = "Euclidean Complete")
legend("topright", legend = list1, cex = parameters$ce +
0.2, fill = list2)
par(opt)
}
if (toPDF) {
dev.off()
}
return(list(Corr.ward = clust.cor.ward, Corr.avg = clust.cor.average,
Corr.compl = clust.cor.complete, Euclid.ward = clust.euclid.ward,
Euclid.avg = clust.euclid.average, Euclid.compl = clust.euclid.complete))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.