我在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中所述),我得到以下树

以下是用于生成它的代码: …
我想提取树状图或群集的节点的层次结构。
例如,在下一个示例中:
library(dendextend)
dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram
dend15 %>% plot
Run Code Online (Sandbox Code Playgroud)
根据节点在树状图中的位置对其进行分类(请参见下图)
(图摘自dendextend包的教程)
我想获取每个最终叶子的所有节点作为下一个输出:(标签从左到右,从下到上排序)
hierarchical structure
leaf_1: 3-2-1
leaf_2: 4-2-1
leaf_3: 6-5-1
leaf_4: 8-7-5-1
leaf_5: 9-7-5-1
Run Code Online (Sandbox Code Playgroud)
提前致谢,
我正在尝试使用名为 dendextend 的出色 R 包来绘制树状图并根据一组先前定义的组为其分支和标签着色。我已经阅读了 Stack Overflow 中的答案以及 dendextend 小插图的常见问题解答,但我仍然不确定如何实现我的目标。
假设我有一个数据框,其中第一列包含用于聚类的个人姓名,然后是几列包含要分析的因素,最后一列包含每个人的组信息(见下表) .
individual 282856 282960 283275 283503 283572 283614 284015 group
pat15612 0 0 0 0 0 0 0 g2
pat38736 0 0 0 0 0 0 0 g2
pat38740 0 0 0 0 0 1 0 g2
pat38742 0 0 0 0 0 1 0 g4
pat38743 0 0 1 0 0 1 0 g3
pat38745 0 0 1 0 1 0 0 g4
pat38750 0 0 0 1 …Run Code Online (Sandbox Code Playgroud) 我如何为树形图中添加的一些彩色条添加标签?
下面的代码将显示我为瞄准任务所做的两次尝试,即在彩色条的标签中将值1连接到红色并将值0连接到颜色白色.
# replacing the graphic window parameter so the color bars would fit
par( oma = c(0,1,1,1), mgp = c(1,0.5,0), mar = c(10,2,2,2) )
# load necessary packages
library( squash )
library( dendextend )
# "initializatin"
data("mtcars")
myDend <- as.dendrogram(hclust(dist(mtcars)))
# creating the numeric & color matrix used for
# (attempted) labels & colors bars, respectively
myStatus <- cbind(mtcars$vs,mtcars$am)
myColors <- matrix(c("mintcream","firebrick3")[1 + myStatus],ncol = 2)
myColors <- matrix(c("mintcream","firebrick3")[1 + cbind(mtcars$vs,mtcars$am)], …Run Code Online (Sandbox Code Playgroud) 这是我的数据
df<- structure(list(name = structure(c(2L, 12L, 1L, 16L, 14L, 10L,
9L, 5L, 15L, 4L, 8L, 13L, 7L, 6L, 3L, 11L), .Label = c("All",
"Bab", "boro", "bra", "charli", "delta", "few", "hora", "Howe",
"ist", "kind", "Kiss", "myr", "No", "TT", "where"), class = "factor"),
value = c(1.251, -1.018, -1.074, -1.137, 1.018, 1.293, 1.022,
-1.008, 1.022, 1.252, -1.005, 1.694, -1.068, 1.396, 1.646,
1.016)), .Names = c("name", "value"), class = "data.frame", row.names = c(NA,
-16L))
Run Code Online (Sandbox Code Playgroud)
这是我所做的
d <- dist(as.matrix(df$value),method = "euclidean")
#compute cluster membership
hcn …Run Code Online (Sandbox Code Playgroud) 使用hclust函数在R中执行层次聚类时.你怎么知道最终合并的高度?
所以要澄清一些R默认数据:
hc <- hclust(dist(USArrests))
dendrogram1 = as.dendrogram(hc)
plot(hc)
Run Code Online (Sandbox Code Playgroud)
将导致具有所有聚类信息的变量hc.

和树状图:

正如您在树形图上看到的那样,最终合并发生在> 200(约300)的高度.但是树状图是如何知道的?此信息不在hc.height变量中,也不在dendrogram1变量中.提到的最高合并是169.

如果dendrogram1变量不包含此信息,则绘图函数如何知道合并必须在300的高度发生?

我问这个是因为我需要这个数字(+ - 300)用于其他应用程序,从图中读取它是非常不切实际的.
感谢任何愿意帮助的人!
我使用cutree()将我的hclust()树聚集到几个组中.现在我想要一个函数来将几个组成员hclust()作为一个hclust()......还有:
我将一棵树切成168组,我想要168个hclust()树......我的数据是1600*1600矩阵.
我的数据太大了,所以我给你举个例子
m<-matrix(1:1600,nrow=40)
#m<-as.matrix(m) // I know it isn't necessary here
m_dist<-as.dist(m,diag = FALSE )
m_hclust<-hclust(m_dist, method= "average")
plot(m_hclust)
groups<- cutree(m_hclust, k=18)
Run Code Online (Sandbox Code Playgroud)
现在我要绘制18棵树......一组树.我试过很多..