如何在R中更改树状图标签 [英] How to change dendrogram labels in r

查看:446
本文介绍了如何在R中更改树状图标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个树状图.它基于使用hclust的层次聚类.我正在为不同颜色的标签着色,但是当我尝试使用dendrogram = dendrogram %>% set("labels", dataframe$column)更改我的数据图的标签(到群集所基于的数据框的行)时,标签被替换,但是位置错误.例如:

I have a dendrogram in R. It is based on hierachical clustering using 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. As example:

我的树状图如下:

 ___|___
|      _|_
|     |   | 
|     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

我想要的是这个东西

    ___|___
   |      _|_
   |     |   | 
   |     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?

推荐答案

在创建的hclust对象cl中,您有一个名为"order"的元素,其中包含这些元素所在的顺序.树状图.

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.

如果要更改标签,则需要按相同顺序(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:

df$column2[cl$order]

这篇关于如何在R中更改树状图标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆