【问题标题】:How to use identify to a horizontal dendrogram of class "dendrogram" in R如何在R中使用识别类“树状图”的水平树状图
【发布时间】:2015-12-15 10:06:16
【问题描述】:

我正在使用 identify 来探索 R 中树状图中集群的特定特征。通过使用“hclust”对象,Identify 工作得非常好,但我需要它用于 “树状图”类的水平树状图 而不是 'hclust'。我安装了包dendextend,它通常应该将识别功能扩展到类树状图的对象和水平树状图(http://rpackages.ianhowson.com/cran/dendextend/man/identify.dendrogram.html)。对于我的特定数据集,识别适用于垂直树状图(类树状图),但不适用于水平树状图。 我总是得到的错误是:

Error in rect.dendrogram(x, k = k, x = X$x, cluster = cluster[, k - 1],  : 
k must be between 2 and 10

请在此处找到一个可重现且简化的示例:

#Install packages
install.packages(c("TraMineR","dendextend"))
#Load packages
library(TraMineR)
library(dendextend)

#Create fake dataset (each row is a sequence of characters)
a <- c(rep('A',50), rep('B',50))
seqdf <- rbind(a=a, b=sample(a), c=sample(a), d=sample(a), e=sample(a), f=sample(a),g=sample(a),h=sample(a),
i=sample(a), j=rep('A',100),k=rep('B',100),l=sample(a)) 
colnames(seqdf)<- paste(rep('a',100),c(1:100),sep='') 

#Turn it into a sequence object 
seq_def <- seqdef(seqdf, 1:100, id = rownames(seqdf), xtstep = 4)

#Calculate the dissimilarity (hamming distance) between sequences 
hd <- seqdist(seq_def, method = "HAM", with.missing = TRUE)
rows<-list(rownames(seqdf),rownames(seqdf))
dimnames(hd) <- rows
#Perform Ward clustering on dissimilarity matrix hd
ward <- hclust(as.dist(hd), method = "ward.D2")     
#Dendrogram object
dend <- as.dendrogram(ward) 

#Horizontal dendrogram 
plot(dend, horiz=TRUE)
identify(dend, horiz=TRUE) # HERE IDENTIFY GIVES AN ERROR

#Vertical dendrogram
plot(dend)
identify(dend) # this works, there is no error

希望有人知道如何解决这个问题。

最好的,

【问题讨论】:

    标签: r dendrogram identify dendextend


    【解决方案1】:

    当您单击“太靠近”屏幕边缘时,这是识别功能(例如,identify.hclust)的一般行为。如果您将运行(并在叶子附近单击),您可以看到这个:

    plot(ward)
    identify(ward, MAXCLUSTER = 12) 
    

    我同意你的观点,这是一种有点烦人的行为(因为我们并不总是能够准确地点击我们想要的位置)。所以我有added to the dendextend package a new parameter (stop_if_out),现在默认为 identify.dendrogram 设置为FALSE。这意味着当点击树状图外太远时,该功能将不再停止。 (它适用于垂直和水平图)

    我可能需要一些时间才能将此版本发布到 CRAN,但您可以通过使用 devtools 并运行来轻松访问它:

    install.packages.2 <- function (pkg) if (!require(pkg)) install.packages(pkg);
    install.packages.2('devtools')
    # make sure you have Rtools installed first! if not, then run:
    #install.packages('installr'); install.Rtools()
    devtools::install_github('talgalili/dendextend')
    

    我希望这会有所帮助。

    【讨论】:

    • 非常感谢您的快速响应和准确的解决方案!问题解决了!
    猜你喜欢
    • 2020-11-27
    • 2012-12-16
    • 2014-07-31
    • 2013-06-09
    • 2015-09-05
    • 2017-04-27
    • 2011-11-18
    • 1970-01-01
    相关资源
    最近更新 更多