在R中是否有一种方法可以在Venn图中显示分层聚类?

4
我正在尝试将分层聚类显示为韦恩图或其他有用的显示方式,而不是树状图。我希望能够以许多不同的视图类型显示我的数据。
目前,这样做会绘制一个树状图:
x <- hclust(dist(mtcars))
plot(x)

我应该怎么做才能显示一个类似于这样的聚类图:

https://www.projectrhea.org/rhea/images/3/3b/Lecture23VennClusters_OldKiwi.jpg

或者这个

http://bl.ocks.org/mbostock/7607535

或者任何其他在此示例中展示聚类数据时更有意义的方式。
最好能够在Shiny中完成这项工作,但一个简单的R示例也足以说明问题。谢谢您提前的帮助。

请尝试访问此网站:http://www.ats.ucla.edu/stat/r/faq/venn.htm。 - user3969377
1个回答

3
你展示的图表是聚类图。有不同的方法制作这些图表。以下是一种方法。您可以根据需要更改符号或关闭它们,以及填充。此外,还有用于树状图绘制的选项,即 here
library(cluster)

head(mtcars)
fit <- kmeans(mtcars, 3)  # 3 clusters
aggregate(mtcars, by=list(fit$cluster), mean)
newmtcars <- data.frame(mtcars, fit$cluster)
head(newmtcars)

# plot cluster solution
library(cluster)
clusplot(mtcars, fit$cluster, 
         color=TRUE, shade=TRUE, lines=0)

enter image description here

参考文献:http://www.statmethods.net/advstats/cluster.html https://stats.stackexchange.com/questions/31083/how-to-produce-a-pretty-plot-of-the-results-of-k-means-cluster-analysis

我不确定Venn图与上述图表有何不同。也许需要有重叠的组。这取决于数据和树命令。在这种情况下,可以尝试变化树命令(kmeans),当选择迭代次数时,显示了一个小的重叠。

fit <- kmeans(mtcars, 3, iter.max = 2)  # 3 clusters, low number of iterations
clusplot(mtcars, fit$cluster, 
         color=TRUE, shade=FALSE, lines=0)

enter image description here

使用层次聚类的一种方法是从树中提取群组,然后在结果群组上使用clusplot。
fit <- hclust(dist(mtcars))
groups <- cutree(fit, k=3)
clusplot(mtcars, groups[rownames(mtcars)], 
         color=TRUE, shade=FALSE, lines=0)

如果想要查看树形结构中切割更多的数据段,包括分层树,可以使用cut和clusplot方法。

heir_tree_fit <- hclust(dist(mtcars))
for (ncut in seq(1,10)) {
  group <- cutree(heir_tree_fit, k=ncut)
  clusplot(mtcars, group[rownames(mtcars)], 
           color=TRUE, shade=FALSE, lines=0, main=paste(ncut,"cuts"))
}

这里是2、6和10刀的数字。

enter image description here

enter image description here

enter image description here

您可以使用所有切割来制作一个图。
par(new=FALSE)
for (ncut in seq(1,10)) {
  group <- cutree(heir_tree_fit, k=ncut)
  clusplot(mtcars, group[rownames(mtcars)], 
           color=TRUE, shade=FALSE, lines=0, xlim=c(-5,5),ylim=c(-5,5))
  par(new=TRUE)
}
par(new=FALSE)

另一种制作层次聚类的维恩图的方法是从树中提取组,并使用vennDiagram处理结果组。
# To make a Venn diagram
# source("http://bioconductor.org/biocLite.R")
biocLite("limma")
library(limma)
inGrp1 <- groups==1
inGrp2 <- groups==2
inGrp3 <- groups==3
vennData <- cbind(inGrp1, inGrp2, inGrp3)

aVenn <- vennCounts(vennData)
vennDiagram(aVenn)

VennDiagramMTCAR


这很酷,但 OP 正在寻找一种使用类似 Venn 图的方式来可视化分层聚类的方法。 - jarfa
@jarfa 增加了一种使用 Venn 图来可视化分层聚类的方法。 - user3969377
OP想要重新创建的(我也很想看到的)是类似于https://www.projectrhea.org/rhea/images/3/3b/Lecture23VennClusters_OldKiwi.jpg的东西。对于k-means算法来说,维恩图并不是很有趣。有趣的是一个图表,展示了一个给定点具有层次类别集合的方式。就像带有空间组件的树状图。 - jarfa
@jarfa 只需使用 cut tree,然后使用 clusplot,参见上文。 - user3969377
哇,非常感谢,这非常有帮助。现在我只需要将它转换成Shiny。当我完成后,我会分享我的结果。与此同时,非常感谢给出了如此棒的答案。 - user3357415

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接