简体   繁体   English

如何更改r中的树状图标签

[英]How to change dendrogram labels in r

I have a dendrogram in R. It is based on hierachical clustering using hclust. 我在R中有一个树形图。它基于使用hclust的分层聚类。 I am colouring labels that are different in different colours, but when I try changing the labels of my dedrogram (to the rows of the dataframe the cluster is based on) using dendrogram = dendrogram %>% set("labels", dataframe$column) the labels are replaced, but in the wrong positions. 我正在着色不同颜色的标签,但是当我尝试使用dendrogram = dendrogram %>% set("labels", dataframe$column) )更改我的dedrogram的标签(到群集所基于的数据帧的行)时dendrogram = dendrogram %>% set("labels", dataframe$column)标签被替换,但位置错误。 As example: 例如:

My dendrogram looks like this: 我的树形图看起来像这样:

 ___|___
|      _|_
|     |   | 
|     1   0
2

when I now try changing the labels like specified above, the labels are changed, but they are applied from left to right in their order in the dataframe. 当我现在尝试更改上面指定的标签时,标签会更改,但它们会按照数据框中的顺序从左到右应用。 If we assume my original dataframe looks like this 如果我们假设我的原始数据框看起来像这样

df:
   Column1  Column2
0     1        A
1     2        B
2     3        C

what I want to have is this: 我想要的是这个:

    ___|___
   |      _|_
   |     |   | 
   |     B   A
   C

But what I actually get is: 但我真正得到的是:

    ___|___
   |      _|_
   |     |   | 
   |     B   C
   A   

the clustering of the data and their transformation into dendrogram was done as follows: 数据的聚类及其转化为树状图的过程​​如下:

> d <- stringdistmatrix(df$Column1, df$Column1)
> cl <- hclust(as.dist(d))
> dend = as.dendrogram(cl)

Can anybody tell me how I can label my dendrogram with the values of another column based on the index? 谁能告诉我如何用基于索引的另一列的值标记我的树状图?

The dendextend package allows you to directly update dendrograms (as well as hclust), by using the following: dendextend包允许您使用以下内容直接更新树形图(以及hclust):

x <- c(1:5)
dend <- as.dendrogram(hclust(dist(x)))

if(!require(dendextend)) install.packages("dendextend")
library("dendextend")

labels(dend)
labels(dend) <- c(21:25)
labels(dend)

In the hclust object you've created, cl , you have an element named "order" that contains the order in which the elements are in the dendrogram. 在您创建的hclust对象中, cl ,您有一个名为“order”的元素,其中包含元素在树形图中的顺序。

If you want to change the labels, you need to put the new labels in the same order ( cl$order ), so the "new" dendrogram is right: 如果要更改标签,则需要以相同的顺序( cl$order )放置新标签,因此“新”树形图是正确的:

df$column2[cl$order]

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

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