简体   繁体   English

树形图边缘(分支)颜色匹配尖端(叶子)颜色(猿包装)

[英]Dendrogram edge (branch) colors to match tip (leaf) colors (ape package)

I'm trying to add color to the edges (lines) of a phylogeny-type plot in R using the plot.phylo command from the ape package. 我正在尝试使用ape包中的plot.phylo命令为R中的系统发育类型图的边(线)添加颜色。 This example is for a "fan" type plot, though I expect the approach would be the same with a "phylogram type" or whatever. 这个例子是针对“粉丝”类型的情节,虽然我希望这种方法与“phylogram类型”或其他类似。

library('ape')
hc <- hclust(dist(USArrests), "ave")
plot(as.phylo(hc), type="fan")

Adding colour to the tips (labels) based on a set of groups is no problem with the tip.color option combined with cutree command. 将tip.color选项与cutree命令结合使用,根据一组组向tip(标签)添加颜色是没有问题的。

hc.cuts <- cutree(hc, k=5)
plot(as.phylo(hc), type="fan", tip.color=rainbow(5)[hc.cuts])

The edge.color option defines the colors of the edges, but not in a logincal manner when many colours are desired. edge.color选项定义边缘的颜色,但在需要许多颜色时不以logincal方式定义。

plot(as.phylo(hc), type="fan", tip.color=rainbow(5)[hc.cuts], edge.color=rainbow(5)[hc.cuts])

However, I would like the edges to match the terminal tip colours once that branch of the dendrogram is destined for a given group. 但是,一旦树形图的分支指定给定的组,我希望边缘匹配终端尖端颜色。 In the given example, towards the red & blue groups, the first levels of edges would stay black (as it's headed for two groups: red and blue), but edges beyond this would be colored the same as the eventual tip color. 在给定的示例中,朝向红色和蓝色组,第一级边缘将保持黑色(因为它朝向两组:红色和蓝色),但是超出此范围的边缘将与最终的尖端颜色相同。

I suspect the key lies in figuring out the ordering of the $edge values in the as.phylo object, but I can't figure it myself. 我怀疑关键在于弄清楚as.phylo对象中$ edge值的排序,但我自己无法想象。 Thanks. 谢谢。

As @maj suggested in a comment, dendextend can help you out if you don't mind using that package. 正如@maj在评论中建议的那样,如果您不介意使用该软件包, dendextend可以帮助您。 It is very flexible and has extensive documentation and vignettes. 它非常灵活,拥有丰富的文档和插图。

Here's an example minimally adapted from the dendextend FAQ . 这是一个最低限度地改编自dendextend FAQ的示例。

# install.packages("dendextend")
# install.packages("circlize")

library(dendextend)
library(circlize)

hc <- hclust(dist(USArrests))
dend <- as.dendrogram(hc)

num_clades <- 5

dend <- dend %>% 
  color_branches(k=num_clades, col=rainbow) %>% 
  color_labels(k=num_clades, col=rainbow)

par(mar = rep(0, 4))
circlize_dendrogram(dend, dend_track_height = 0.8) 

It outputs 它输出

在此输入图像描述

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

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