在R dendextend中比较两个树状图时如何给连接线着色

vc6uscn9  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(100)

我正在使用dendextendtanglegram来比较两个树状图。几乎所有的工作都正常,包括为节点标签着色以对应聚类。不正常的是,我希望所有的连接线(从一个树状图中的节点标签到另一个树状图中的节点标签)都是黑色的。目前它们是随机着色的,这使得很难解释。
这里是R脚本。让我好奇的是,如果我在RGi中运行它,它是正确的(所有连接线都是黑色的),但png有各种颜色的连接线。

suppressPackageStartupMessages(library(dendextend))
library(dendextend)

# for StackOverflow question, data is here rather than loaded from csv file
hab1 <- data.frame(matrix(c(100,90.6,88.9,89.2,91.2,98.2,91,55.9,91.5,97.2,90.6,100,93.9,85.3,98.3,90.1,96.2,53,88.7,91.6,88.9,93.9,100,82.9,94.4,88,93.4,51.9,87.1,90.4,89.2,85.3,82.9,100,86.6,89.8,85.2,60.7,95.8,91,91.2,98.3,94.4,86.6,100,90.6,96.4,53.4,89.2,92.2,98.2,90.1,88,89.8,90.6,100,90.4,56,91.8,97,91,96.2,93.4,85.2,96.4,90.4,100,52.4,88.6,92,55.9,53,51.9,60.7,53.4,56,52.4,100,59.8,56,91.5,88.7,87.1,95.8,89.2,91.8,88.6,59.8,100,93.3,97.2,91.6,90.4,91,92.2,97,92,56,93.3,100),nrow=10,ncol=10))

# set the column names, which are used for node labels
colnames(hab1) <-    c("W01","W02","W03","W04","W05","W06","W07","W08","W09","W10")

hclust1 <- hclust(as.dist(100 - hab1), method="average")
dend1 <- as.dendrogram(hclust1)

hab2 <- data.frame(matrix(c(100,89.5,87.4,88.1,90.1,96.4,89.7,55.1,89.9,96,89.5,100,93.3,85.3,98.3,89.5,96,52.9,88.2,91.6,87.4,93.3,100,82.4,93.9,87,92.7,51.5,86.1,89.9,88.1,85.3,82.4,100,86.6,89.3,85.1,60.6,95.2,91,90.1,98.3,93.9,86.6,100,90.1,96.2,53.3,88.7,92.2,96.4,89.5,87,89.3,90.1,100,89.7,55.5,90.7,96.4,89.7,96,92.7,85.1,96.2,89.7,100,52.2,88,91.8,55.1,52.9,51.5,60.6,53.3,55.5,52.2,100,59.4,55.9,89.9,88.2,86.1,95.2,88.7,90.7,88,59.4,100,92.7,96,91.6,89.9,91,92.2,96.4,91.8,55.9,92.7,100),nrow=10,ncol=10))

# set the column names, which are used for node labels
colnames(hab2) <- c("W01","W02","W03","W04","W05","W06","W07","W08","W09","W10")

hclust2 <- hclust(as.dist(100 - hab2), method="average")
dend2 <- as.dendrogram(hclust2)

# colors for the node labels
colors_to_use1 <- c("purple","orange","blue","darkolivegreen","orange","purple","magenta","red","darkolivegreen","cyan")
colors_to_use2 <- c("purple","orange","blue","darkolivegreen","orange","purple","magenta","red","darkolivegreen","cyan")

# sort the colors based on their order in dend1
colors_to_use_dend1 <- colors_to_use1[order.dendrogram(dend1)]
labels_colors(dend1) <- colors_to_use_dend1

# sort the colors based on their order in dend2
colors_to_use_dend2 <- colors_to_use2[order.dendrogram(dend2)]
labels_colors(dend2) <- colors_to_use_dend2

dends_1_2 <- dendlist(dend1, dend2)
x <- dends_1_2 %>% untangle(method = "step2side") %>% tanglegram(color_lines = c("black"))

png("Exclude vs not exclude.png")
x %>% plot(main = "Exclude vs not exclude")

dev.off( )

字符串
我做错了什么?我试过使用common_subtrees_color_lines = FALSE的tanglegram,但无济于事。

mm9b1k5b

mm9b1k5b1#

根据tanglegram attributescolor_lines是“连接标签的线条的颜色向量。如果颜色短于标签的数量,它们将被回收(并发出警告)。向量中的颜色将自下而上应用于线条。”
因此,您需要将color_lines设置为您需要的颜色。

tanglegram( ..., color_lines="black", ...)

字符串

相关问题