简体   繁体   English

R rect.hclust:树状图中的矩形太高

[英]R rect.hclust: rectangles too high in dendogram

I asked a number of different experts to sort 92 objects based on their similarity.我请了许多不同的专家根据它们的相似性对 92 个对象进行排序。 Based on their answers, I constructed a 92 x 92 dissimilarity matrix.根据他们的回答,我构建了一个 92 x 92 的相异矩阵。 in R, I examined this matrix using the following commands:在 R 中,我使用以下命令检查了这个矩阵:

cluster1 <- hclust(as.dist(DISS_MATRIX), method = "average") 
plot(cluster1, cex=.55)

To highlight the clusters, I wanted to draw rectangles around them:为了突出显示集群,我想在它们周围绘制矩形:

rect.hclust(cluster1, k = 3, border = "red")

The result is as follows:结果如下:

在此处输入图片说明

However, when the objects have longer names ("AAAAAAAAAAAAAAAA43" instead of "A43") then the formating is off:但是,当对象具有更长的名称(“AAAAAAAAAAAAAAAA43”而不是“A43”)时,格式将关闭:

rownames(DISS_MATRIX) <- paste0(rep("AAAAAAAAAAAAAAAAAAAAAAAAAAAA",92),1:92)
colnames(DISS_MATRIX) <- paste0(rep("AAAAAAAAAAAAAAAAAAAAAAAAAAAA",92),1:92)
cluster1 <- hclust(as.dist(DISS_MATRIX), method = "average") 
plot(cluster1, cex=.55)
rect.hclust(cluster1, k = 3, border = "red")

This can be seen by the resulting dendogram.这可以通过生成的树状图看出。

在此处输入图片说明

The rectangles seem to have moved up to the end of the dendogram.矩形似乎已经移动到树状图的末端。 Not nice.不太好。 I assume this glitch must have been due to the long names of 92 objects in the dissimilarity matrix.我认为这个故障一定是由于相异矩阵中 92 个对象的长名称造成的。 It may also not seem very relevant.它也可能看起来不太相关。 Just make sure your objects have names short enough.只要确保您的对象的名称足够短。

However, due to different reasons I want my objects to have their original (ieadmittedly long) names.但是,由于不同的原因,我希望我的对象具有它们原来的(即长的)名称。 This graph is for a presentation and thus I do not want to work with codes.此图用于演示,因此我不想使用代码。 I also do not want to use any other package since I generally find hclust quite easy to use.我也不想使用任何其他包,因为我通常发现 hclust 很容易使用。 However, I do not find any way to position rectangles within the rect.hclust command.但是,我没有找到在 rect.hclust 命令中定位矩形的任何方法。 Hence, what can I do to position the rectangles into the dendogram even if object names are long?因此,即使对象名称很长,我该怎么做才能将矩形定位到树状图中? Thanks.谢谢。

You wrote that "I also do not want to use any other package since I generally find hclust quite easy to use."您写道:“我也不想使用任何其他包,因为我通常发现 hclust 非常易于使用。”

While hclust is great for creating the hierarchical clustering object it does not support much in terms of plotting.虽然 hclust 非常适合创建层次聚类对象,但它在绘图方面支持不多。 Once you have the hclust output, it is better to change it to dendrogram (using as.dendrogram ) for visualizations (since it is better suited for that).获得 hclust 输出后,最好将其更改为 dendrogram(使用as.dendrogram )以进行可视化(因为它更适合as.dendrogram )。 There is no way to do what you want without using sophisticated code, which is packed in a package, this is the best route (IMHO) for you to move forward.如果不使用打包在包中的复杂代码,就无法做您想做的事情,这是您前进的最佳途径(恕我直言)。 (I know because I wrote rect.dendrogram, and it took a lot of work to get it to work the way you want it) (我知道是因为我写了 rect.dendrogram,而且花了很多工作才能让它按照你想要的方式工作)

The dendextend R package allows many functions for manipulating and visualizing dendrograms (see the vignette here ). dendextend R 包允许使用许多函数来操作和可视化树状图(请参阅此处的小插图)。 Specifically, the rect.dendrogram function can handle such cases as you asked about (with having long labels).具体来说, rect.dendrogram 函数可以处理您询问的这种情况(具有长标签)。 For example (I've added color_branches and color_labels for the fun of it):例如(为了好玩,我添加了 color_branches 和 color_labels ):

library(dendextend)
hc <- mtcars[, c("mpg", "disp")] %>% dist %>% hclust(method = "average") 
dend <- hc %>% as.dendrogram %>% hang.dendrogram
# let's make the text longer
labels(dend)[1] <- "AAAAAAAAAAAAAAAAAAAAA"

par(mar = c(15,2,1,1))
dend %>% color_branches(k=3) %>% color_labels(k=3) %>% plot
dend %>% rect.dendrogram(k=3)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM