nov*_*eek 4 plot r cluster-analysis dendrogram dendextend
我在R中有一个24行和10,000列的数字矩阵.该矩阵的行名基本上是文件名,我从中读取了对应于24行中每一行的数据.除此之外,我有一个单独的因子列表,包含24个entires,指定24个文件所属的组.有3组 - 醇类,碳氢化合物和酯类.它们所属的名称和相应组如下所示:
> MS.mz
[1] "int-354.19" "int-361.35" "int-368.35" "int-396.38" "int-408.41" "int-410.43" "int-422.43"
[8] "int-424.42" "int-436.44" "int-438.46" "int-452.00" "int-480.48" "int-648.64" "int-312.14"
[15] "int-676.68" "int-690.62" "int-704.75" "int-312.29" "int-326.09" "int-326.18" "int-326.31"
[22] "int-340.21" "int-340.32" "int-352.35"
> MS.groups
[1] Alcohol Alcohol Alcohol Alcohol Hydrocarbon Alcohol Hydrocarbon Alcohol
[9] Hydrocarbon Alcohol Alcohol Alcohol Ester Alcohol Ester Ester
[17] Ester Alcohol Alcohol Alcohol Alcohol Alcohol Alcohol Hydrocarbon
Levels: Alcohol Ester Hydrocarbon
Run Code Online (Sandbox Code Playgroud)
我想生成一个树形图来查看矩阵中的数据是如何聚类的.所以,我使用了以下命令:
require(vegan)
dist.mat<-vegdist(MS.data.scaled.transposed,method="euclidean")
clust.res<-hclust(dist.mat)
plot(clust.res)
Run Code Online (Sandbox Code Playgroud)
我得到了一个树状图.现在我想根据它们所属的组(即酒精,碳氢化合物或酯类)为树形图中的文件名着色.我查看了论坛上发布的不同例子
,但无法为我的数据实现它.我不确定如何将row.names与MS.groups相关联以获得树形图中的彩色名称.
在使用dendextend生成树时(如https://nycdatascience.com/wp-content/uploads/2013/09/dendextend-tutorial.pdf中所述),我得到以下树

以下是用于生成它的代码:
require(colorspace)
d_SIMS <- dist(firstpointsample5[,-1])
hc_SIMS <- hclust(d_SIMS)
labels(hc_SIMS)
dend_SIMS <- as.dendrogram(hc_SIMS)
SIMS_groups <- rev(levels(firstpointsample5[, 1]))
dend_SIMS <- color_branches(dend_SIMS, k = 3, groupLabels = SIMS_groups)
is.character(labels(dend_SIMS))
plot(dend_SIMS)
labels_colors(dend_SIMS) <- rainbow_hcl(3)[sort_levels_values(as.numeric(firstpointsample5[,1])[order.dendrogram(dend_SIMS)])]
labels(dend_SIMS) <- paste(as.character(firstpointsample5[, 1])[order.dendrogram(dend_SIMS)],"(", labels(dend_SIMS), ")", sep = "")
dend_SIMS <- hang.dendrogram(dend_SIMS, hang_height = 0.1)
dend_SIMS <- assign_values_to_leaves_nodePar(dend_SIMS, 0.5,"lab.cex")
par(mar = c(3, 3, 3, 7))
plot(dend_SIMS, main = "Clustered SIMS dataset\n (the labels give the true m/z groups)",horiz = TRUE, nodePar = list(cex = 0.007))
legend("topleft", legend = SIMS_groups, fill = rainbow_hcl(3))
Run Code Online (Sandbox Code Playgroud)
我怀疑你正在寻找的功能是color_labels或者get_leaves_branches_col.标签所基于的第一种颜色cutree(如color_branchesdo)和第二种颜色允许您获取每个叶子的分支的颜色,然后使用它来为树的标签着色(如果您使用不寻常的方法为分支着色(如在使用时发生branches_attr_by_labels.例如:
# define dendrogram object to play with:
hc <- hclust(dist(USArrests[1:5,]), "ave")
dend <- as.dendrogram(hc)
library(dendextend)
par(mfrow = c(1,2), mar = c(5,2,1,0))
dend <- dend %>%
color_branches(k = 3) %>%
set("branches_lwd", c(2,1,2)) %>%
set("branches_lty", c(1,2,1))
plot(dend)
dend <- color_labels(dend, k = 3)
# The same as:
# labels_colors(dend) <- get_leaves_branches_col(dend)
plot(dend)
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,你应该总是看一下这个set函数,看看你的树形图可以做些什么(这样就省去了记住所有不同函数名称的麻烦).