我正在使用dendextend
'stanglegram
来比较两个树状图。几乎一切正常,包括为节点标签着色以对应于集群。不起作用的是我希望所有连接线(从一个树状图中的节点标签到另一个树状图中的节点标签)都是黑色的。目前它们的颜色是随机的,因此难以解释。
这是R脚本。令我好奇的是,如果我在 RGui 中运行它,它是正确的(所有连接线都是黑色的),但是 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
for tanglegram,但无济于事。