I clustered my hclust() tree into several groups with cutree(). Now I want a function to hclust() the several groupmembers as a hclust()... ALSO:
I cut one tree into 168 groups and I want 168 hclust() trees... My data is a 1600*1600 matrix.
My data is tooooo big so I give you an example
m<-matrix(1:1600,nrow=40)
#m<-as.matrix(m) // I know it isn't necessary here
m_dist<-as.dist(m,diag = FALSE )
m_hclust<-hclust(m_dist, method= "average")
plot(m_hclust)
groups<- cutree(m_hclust, k=18)
Now I want to plot 18 trees... one tree for one group. I've tried a lot..
I warn you upfront that for such large trees, probably most solutions would be a bit slow to run. But here is one solution (using the dendextend R package):
m<-matrix(1:1600,nrow=40)
#m<-as.matrix(m) // I know it isn't necessary here
m_dist<-as.dist(m,diag = FALSE )
m_hclust<-hclust(m_dist, method= "complete")
plot(m_hclust)
groups <- cutree(m_hclust, k=18)
# Get dendextend
install.packages.2 <- function (pkg) if (!require(pkg)) install.packages(pkg);
install.packages.2('dendextend')
install.packages.2('colorspace')
library(dendextend)
library(colorspace)
# I'll do this to just 4 clusters for illustrative purposes
k <- 4
cols <- rainbow_hcl(k)
dend <- as.dendrogram(m_hclust)
dend <- color_branches(dend, k = k)
plot(dend)
labels_dend <- labels(dend)
groups <- cutree(dend, k=4, order_clusters_as_data = FALSE)
dends <- list()
for(i in 1:k) {
labels_to_keep <- labels_dend[i != groups]
dends[[i]] <- prune(dend, labels_to_keep)
}
par(mfrow = c(2,2))
for(i in 1:k) {
plot(dends[[i]],
main = paste0("Tree number ", i))
}
# p.s.: because we have 3 root only trees, they don't have color (due to a "missing feature" in the way R plots root only dendrograms)
Let's do it again on a "nicer" tree:
m_dist<-dist(mtcars,diag = FALSE )
m_hclust<-hclust(m_dist, method= "complete")
plot(m_hclust)
# Get dendextend
install.packages.2 <- function (pkg) if (!require(pkg)) install.packages(pkg);
install.packages.2('dendextend')
install.packages.2('colorspace')
library(dendextend)
library(colorspace)
# I'll do this to just 4 clusters for illustrative purposes
k <- 4
cols <- rainbow_hcl(k)
dend <- as.dendrogram(m_hclust)
dend <- color_branches(dend, k = k)
plot(dend)
labels_dend <- labels(dend)
groups <- cutree(dend, k=4, order_clusters_as_data = FALSE)
dends <- list()
for(i in 1:k) {
labels_to_keep <- labels_dend[i != groups]
dends[[i]] <- prune(dend, labels_to_keep)
}
par(mfrow = c(2,2))
for(i in 1:k) {
plot(dends[[i]],
main = paste0("Tree number ", i))
}
# p.s.: because we have 3 root only trees, they don't have color (due to a "missing feature" in the way R plots root only dendrograms)