如何使用同一数据框中另一列的分类变量在 R 中的 hclust 中标记树状图的叶子

发布于 2025-01-10 02:31:22 字数 957 浏览 5 评论 0原文

我想更改树状图叶子的默认标签,以匹配使用 dplyr group_bysummarise 生成的数据框中的分类变量列功能。这是数据框的屏幕截图。 dataframe我想使用“m”列变量作为树状图的标签。

这是生成树状图的代码(sfdf_lop 是数据帧)

csfdf_lop <- hclust(dist(sfdf_lop[, -1]), method = "complete")
plot(csfdf_lop)

,输出如下所示: 树状图

如何使用“m”列中的变量来标记叶子,而不是默认编号的叶子?

编辑下面是使用建议代码

tempdf<- as.data.frame(sfdf_lop)
row.names(tempdf)<- tempdf$m
csfdf_lop <- hclust(dist(tempdf[, -1]), method = "complete")
plot(csfdf_lop)

dendrogram

I would like to change the default labels for the leaves of a dendrogram to match a categorical variable column in a dataframe generated using dplyr group_by and summarise functions. This is a screenshot of the dataframe.
dataframe
I would like to use the 'm' column variables as the labels for the dendrogram.

This is the code to generate the dendrogram (sfdf_lop is the dataframe)

csfdf_lop <- hclust(dist(sfdf_lop[, -1]), method = "complete")
plot(csfdf_lop)

and the output looks like this:
dendrogram

How do I use the variables in the column 'm' to label the leaves, in place of the default numbered leaves?

Edit Below is the result of using the suggested code

tempdf<- as.data.frame(sfdf_lop)
row.names(tempdf)<- tempdf$m
csfdf_lop <- hclust(dist(tempdf[, -1]), method = "complete")
plot(csfdf_lop)

dendrogram

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-17 02:31:22

如果将数据转换为 data.frame 并将 row.names 定义为等于 M 列。

tempdf<- as.data.frame(sfdf_lop)
row.names(tempdf)<- tempdf$m

csfdf_lop <- hclust(dist(tempdf[, -1]), method = "complete")
plot(csfdf_lop)

在此处输入图像描述

If you convert your data to data.frame and define the row.names to equal column M.

tempdf<- as.data.frame(sfdf_lop)
row.names(tempdf)<- tempdf$m

csfdf_lop <- hclust(dist(tempdf[, -1]), method = "complete")
plot(csfdf_lop)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文