hclust() with cutree…how to plot the cutree() cluster in single hclust()

冷暖自知 提交于 2019-12-24 15:21:40

问题


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..


回答1:


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)



来源:https://stackoverflow.com/questions/34948606/hclust-with-cutree-how-to-plot-the-cutree-cluster-in-single-hclust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!