如何手动创建树状图(或“hclust”)对象? (在 R 中)

发布于 2024-08-22 06:24:18 字数 316 浏览 6 评论 0原文

我有一个作为图像的树状图。由于它不是很大,我可以“手动”将其构建为 R 对象。

所以我的问题是,当我只有树状图图像时,如何手动创建树状图(或“hclust”)对象?

我看到有一个名为“as.dendrogram”的函数,但我找不到如何使用它的示例。

(ps:这篇文章是根据我的问题 在这里

非常感谢, 塔尔

I have a dendrogram given to me as images. Since it is not very large, I can construct it "by hand" into an R object.

So my question is how do I manually create a dendrogram (or "hclust") object when all I have is the dendrogram image?

I see that there is a function called "as.dendrogram" But I wasn't able to find an example on how to use it.

(p.s: This post is following my question from here)

Many thanks,
Tal

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

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

发布评论

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

评论(2

哀由 2024-08-29 06:24:18

我认为您最好创建一个 hclust 对象,然后使用 as.dendrogram 将其转换为树形图,然后尝试直接创建树形图。查看 ?hclust 帮助页面,了解 hclust 对象元素的含义。

这是一个简单的例子,有四个叶子 A、B、C 和 D,首先组合 AB,然后组合 CD,最后组合 AB-CD:

a <- list()  # initialize empty object
# define merging pattern: 
#    negative numbers are leaves, 
#    positive are merged clusters (defined by row number in $merge)
a$merge <- matrix(c(-1, -2,
                    -3, -4,
                     1,  2), nc=2, byrow=TRUE ) 
a$height <- c(1, 1.5, 3)    # define merge heights
a$order <- 1:4              # order of leaves(trivial if hand-entered)
a$labels <- LETTERS[1:4]    # labels of leaves
class(a) <- "hclust"        # make it an hclust object
plot(a)                     # look at the result   

#convert to a dendrogram object if needed
ad <- as.dendrogram(a)

I think you are better of creating an hclust object, and then converting it to a dendrogram using as.dendrogram, then trying to create a dendrogram directly. Look at the ?hclust help page to see the meaning of the elements of an hclust object.

Here is a simple example with four leaves A, B, C, and D, combining first A-B, then C-D, and finally AB-CD:

a <- list()  # initialize empty object
# define merging pattern: 
#    negative numbers are leaves, 
#    positive are merged clusters (defined by row number in $merge)
a$merge <- matrix(c(-1, -2,
                    -3, -4,
                     1,  2), nc=2, byrow=TRUE ) 
a$height <- c(1, 1.5, 3)    # define merge heights
a$order <- 1:4              # order of leaves(trivial if hand-entered)
a$labels <- LETTERS[1:4]    # labels of leaves
class(a) <- "hclust"        # make it an hclust object
plot(a)                     # look at the result   

#convert to a dendrogram object if needed
ad <- as.dendrogram(a)
哭泣的笑容 2024-08-29 06:24:18

完全手动...不推荐用于大型数据集。

tree<-list();
attributes(tree)<-list(members=18,height=3);
class(tree)<-"dendrogram";

tree[[1]]<-list();
attributes(tree[[1]])<-list(members=4,height=2,edgetext="1");
 tree[[1]][[1]]<-list();
 attributes(tree[[1]][[1]])<-list(members=2,height=1,edgetext="H");
  tree[[1]][[1]][[1]]<-list();
  attributes(tree[[1]][[1]][[1]])<-list(members=1,height=0,edgetext="H",label="1HH",leaf=TRUE);
  tree[[1]][[1]][[2]]<-list();
  attributes(tree[[1]][[1]][[2]])<-list(members=1,height=0,edgetext="T",label="1HT",leaf=TRUE);
 tree[[1]][[2]]<-list();
 attributes(tree[[1]][[2]])<-list(members=2,height=1,edgetext="T");
  tree[[1]][[2]][[1]]<-list();
  attributes(tree[[1]][[2]][[1]])<-list(members=1,height=0,edgetext="H",label="1TH",leaf=TRUE);
  tree[[1]][[2]][[2]]<-list();
  attributes(tree[[1]][[2]][[2]])<-list(members=1,height=0,edgetext="T",label="1TT",leaf=TRUE);
tree[[2]]<-list();
attributes(tree[[2]])<-list(members=2,height=2,edgetext="2");
 tree[[2]][[1]]<-list();
 attributes(tree[[2]][[1]])<-list(members=1,height=0,edgetext="H",label="2H",leaf=TRUE);
 tree[[2]][[2]]<-list();
 attributes(tree[[2]][[2]])<-list(members=1,height=0,edgetext="T",label="2T",leaf=TRUE);
tree[[3]]<-list();
attributes(tree[[3]])<-list(members=4,height=2,edgetext="3");
 tree[[3]][[1]]<-list();
 attributes(tree[[3]][[1]])<-list(members=2,height=1,edgetext="H");
  tree[[3]][[1]][[1]]<-list();
  attributes(tree[[3]][[1]][[1]])<-list(members=1,height=0,edgetext="H",label="3HH",leaf=TRUE);
  tree[[3]][[1]][[2]]<-list();
  attributes(tree[[3]][[1]][[2]])<-list(members=1,height=0,edgetext="T",label="3HT",leaf=TRUE);
 tree[[3]][[2]]<-list();
 attributes(tree[[3]][[2]])<-list(members=2,height=1,edgetext="T");
  tree[[3]][[2]][[1]]<-list();
  attributes(tree[[3]][[2]][[1]])<-list(members=1,height=0,edgetext="H",label="3TH",leaf=TRUE);
  tree[[3]][[2]][[2]]<-list();
  attributes(tree[[3]][[2]][[2]])<-list(members=1,height=0,edgetext="T",label="3TT",leaf=TRUE);
tree[[4]]<-list();
attributes(tree[[4]])<-list(members=2,height=2,edgetext="4");
 tree[[4]][[1]]<-list();
 attributes(tree[[4]][[1]])<-list(members=1,height=0,edgetext="H",label="4H",leaf=TRUE);
 tree[[4]][[2]]<-list();
 attributes(tree[[4]][[2]])<-list(members=1,height=0,edgetext="T",label="4T",leaf=TRUE);
tree[[5]]<-list();
attributes(tree[[5]])<-list(members=4,height=2,edgetext="5");
 tree[[5]][[1]]<-list();
 attributes(tree[[5]][[1]])<-list(members=2,height=1,edgetext="H");
  tree[[5]][[1]][[1]]<-list();
  attributes(tree[[5]][[1]][[1]])<-list(members=1,height=0,edgetext="H",label="5HH",leaf=TRUE);
  tree[[5]][[1]][[2]]<-list();
  attributes(tree[[5]][[1]][[2]])<-list(members=1,height=0,edgetext="T",label="5HT",leaf=TRUE);
 tree[[5]][[2]]<-list();
 attributes(tree[[5]][[2]])<-list(members=2,height=1,edgetext="T");
  tree[[5]][[2]][[1]]<-list();
  attributes(tree[[5]][[2]][[1]])<-list(members=1,height=0,edgetext="H",label="5TH",leaf=TRUE);
  tree[[5]][[2]][[2]]<-list();
  attributes(tree[[5]][[2]][[2]])<-list(members=1,height=0,edgetext="T",label="5TT",leaf=TRUE);
tree[[6]]<-list();
attributes(tree[[6]])<-list(members=2,height=2,edgetext="6");
 tree[[6]][[1]]<-list();
 attributes(tree[[6]][[1]])<-list(members=1,height=0,edgetext="H",label="6H",leaf=TRUE);
 tree[[6]][[2]]<-list();
 attributes(tree[[6]][[2]])<-list(members=1,height=0,edgetext="T",label="6T",leaf=TRUE);


windows(width=3,rescale="fixed");
par(ps=8);
plot(rev(tree),center=TRUE,horiz=TRUE);

Completely manual... not recommended for large datasets.

tree<-list();
attributes(tree)<-list(members=18,height=3);
class(tree)<-"dendrogram";

tree[[1]]<-list();
attributes(tree[[1]])<-list(members=4,height=2,edgetext="1");
 tree[[1]][[1]]<-list();
 attributes(tree[[1]][[1]])<-list(members=2,height=1,edgetext="H");
  tree[[1]][[1]][[1]]<-list();
  attributes(tree[[1]][[1]][[1]])<-list(members=1,height=0,edgetext="H",label="1HH",leaf=TRUE);
  tree[[1]][[1]][[2]]<-list();
  attributes(tree[[1]][[1]][[2]])<-list(members=1,height=0,edgetext="T",label="1HT",leaf=TRUE);
 tree[[1]][[2]]<-list();
 attributes(tree[[1]][[2]])<-list(members=2,height=1,edgetext="T");
  tree[[1]][[2]][[1]]<-list();
  attributes(tree[[1]][[2]][[1]])<-list(members=1,height=0,edgetext="H",label="1TH",leaf=TRUE);
  tree[[1]][[2]][[2]]<-list();
  attributes(tree[[1]][[2]][[2]])<-list(members=1,height=0,edgetext="T",label="1TT",leaf=TRUE);
tree[[2]]<-list();
attributes(tree[[2]])<-list(members=2,height=2,edgetext="2");
 tree[[2]][[1]]<-list();
 attributes(tree[[2]][[1]])<-list(members=1,height=0,edgetext="H",label="2H",leaf=TRUE);
 tree[[2]][[2]]<-list();
 attributes(tree[[2]][[2]])<-list(members=1,height=0,edgetext="T",label="2T",leaf=TRUE);
tree[[3]]<-list();
attributes(tree[[3]])<-list(members=4,height=2,edgetext="3");
 tree[[3]][[1]]<-list();
 attributes(tree[[3]][[1]])<-list(members=2,height=1,edgetext="H");
  tree[[3]][[1]][[1]]<-list();
  attributes(tree[[3]][[1]][[1]])<-list(members=1,height=0,edgetext="H",label="3HH",leaf=TRUE);
  tree[[3]][[1]][[2]]<-list();
  attributes(tree[[3]][[1]][[2]])<-list(members=1,height=0,edgetext="T",label="3HT",leaf=TRUE);
 tree[[3]][[2]]<-list();
 attributes(tree[[3]][[2]])<-list(members=2,height=1,edgetext="T");
  tree[[3]][[2]][[1]]<-list();
  attributes(tree[[3]][[2]][[1]])<-list(members=1,height=0,edgetext="H",label="3TH",leaf=TRUE);
  tree[[3]][[2]][[2]]<-list();
  attributes(tree[[3]][[2]][[2]])<-list(members=1,height=0,edgetext="T",label="3TT",leaf=TRUE);
tree[[4]]<-list();
attributes(tree[[4]])<-list(members=2,height=2,edgetext="4");
 tree[[4]][[1]]<-list();
 attributes(tree[[4]][[1]])<-list(members=1,height=0,edgetext="H",label="4H",leaf=TRUE);
 tree[[4]][[2]]<-list();
 attributes(tree[[4]][[2]])<-list(members=1,height=0,edgetext="T",label="4T",leaf=TRUE);
tree[[5]]<-list();
attributes(tree[[5]])<-list(members=4,height=2,edgetext="5");
 tree[[5]][[1]]<-list();
 attributes(tree[[5]][[1]])<-list(members=2,height=1,edgetext="H");
  tree[[5]][[1]][[1]]<-list();
  attributes(tree[[5]][[1]][[1]])<-list(members=1,height=0,edgetext="H",label="5HH",leaf=TRUE);
  tree[[5]][[1]][[2]]<-list();
  attributes(tree[[5]][[1]][[2]])<-list(members=1,height=0,edgetext="T",label="5HT",leaf=TRUE);
 tree[[5]][[2]]<-list();
 attributes(tree[[5]][[2]])<-list(members=2,height=1,edgetext="T");
  tree[[5]][[2]][[1]]<-list();
  attributes(tree[[5]][[2]][[1]])<-list(members=1,height=0,edgetext="H",label="5TH",leaf=TRUE);
  tree[[5]][[2]][[2]]<-list();
  attributes(tree[[5]][[2]][[2]])<-list(members=1,height=0,edgetext="T",label="5TT",leaf=TRUE);
tree[[6]]<-list();
attributes(tree[[6]])<-list(members=2,height=2,edgetext="6");
 tree[[6]][[1]]<-list();
 attributes(tree[[6]][[1]])<-list(members=1,height=0,edgetext="H",label="6H",leaf=TRUE);
 tree[[6]][[2]]<-list();
 attributes(tree[[6]][[2]])<-list(members=1,height=0,edgetext="T",label="6T",leaf=TRUE);


windows(width=3,rescale="fixed");
par(ps=8);
plot(rev(tree),center=TRUE,horiz=TRUE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文