简体   繁体   English

在R中绘制漂亮且对齐的彩色树状图

[英]Plot Pretty and aligned Colored Dendrograms in R

I am using the Sparcl package ( https://cran.r-project.org/web/packages/sparcl/sparcl.pdf ) to plot dendrograms in R. In my specific problem, I am clustering the groups according to one criterion, and I want to visualize by coloring based on another criterion (the point of this is to show that the cluster coincides (or does not), with another characteristic. I have been able to do this with the Sparcl package, to highlight the nodes that I want to emphasize: 我正在使用Sparcl包( https://cran.r-project.org/web/packages/sparcl/sparcl.pdf )在R中绘制树状图。在我的特定问题中,我正在根据一个条件对这些组进行聚类,并且我想通过基于另一个标准的着色进行可视化(这一点是为了表明集群与另一个特征重合(或不重合)。我已经能够使用Sparcl包做到这一点,以突出显示我想强调:

df <- read.delim("the_data_matrix.txt");
d <- dist(as.matrix(df))
hc = hclust(d)
y[]='black'
y[list_of_nodes$V1]='red' # This will allow me to color only certain branches red, leaving the others black

If I plot with the standard plotting function, I can control various parameters, such as labels and text size with hang and cex (but cannot color any branches) (In the picture this is "Dendrogram 1"): 如果使用标准绘图功能进行绘图,则可以控制各种参数,例如使用hang和cex进行标签和文本大小设置(但不能为任何分支着色)(在图中为“ Dendrogram 1”):

plot(hc,hang=-10,cex=.1)

On the other hand, if I plot using the ColorDendrogram function within Sparcl, I can get a colored dendrogram, but lose formatting options (In the picture this is "Dendrogram 2"): 另一方面,如果我在Sparcl中使用ColorDendrogram函数进行绘图,则可以得到彩色树状图,但会丢失格式设置选项(在图片中为“ Dendrogram 2”):

ColorDendrogram(hc, y = y, branchlength = 4)

ColorDendrogram gave me errors when I used hang and cex to control text size and placement. 当我使用hang和cex来控制文本大小和位置时,ColorDendrogram给了我错误。 在此处输入图片说明 My Question 我的问题

Does anyone know how to fix this, either within the Sparcl package or another one? 有谁知道如何在Sparcl软件包中或另一个软件包中解决此问题? I would like to have flexibility of color that ColorDendrogram has, but not lose formatting capabilities. 我希望具有ColorDendrogram具有的颜色灵活性,但又不要失去格式化功能。

Try the package dendextend ( vignette ), which should give you all flexibility: 尝试使用dendextendvignette )软件包,它将为您提供所有的灵活性:

library(dendextend)
d1 <- mtcars %>% dist %>% hclust %>% as.dendrogram
d2 <- mtcars %>% dist(method="minkowski") %>% hclust(method="single") %>% as.dendrogram
vals <- grep("Merc", rownames(mtcars), val=T) # highlight branches leading to "Merc..."

par(mfrow=c(2, 1))
d1 %>% set("by_labels_branches_col", value = vals) %>% set("hang_leaves", -10) %>% set("labels_cex", .1) %>% plot
d2 %>% set("by_labels_branches_col", value = vals) %>% plot

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM