使用猿猴笼包装对R中的标签和彩色叶片树状图(系统发育) [英] Label and color leaf dendrogram (phylogeny) in R using ape package
问题描述
在上一篇文章之后( r中的标签和色叶树状图)我有一个后续问题.
Following a previous post (Label and color leaf dendrogram in r) I have a follow-up question.
我的问题与提到的帖子类似,但我想知道是否可以使用猿来完成(例如,plot(as.phylo(fit), type="fan", labelCol)
,因为它具有更多的系统发育类型.
My questions are similar to the post mentioned but I wonder can it be done using ape (e.g., plot(as.phylo(fit), type="fan", labelCol)
as it has more type of phylogeny.
提到的帖子问题是:
-
如何在叶子标签上显示组代码(而不是样本编号)?
How can I show the group codes in leaf label (instead of the sample number)?
我希望为每个代码组分配一种颜色,并根据其为叶子标签着色(可能会出现它们不在同一个分支中的情况,这样我可以找到更多信息)?
I wish to assign a color to each code group and colored the leaf label according to it (it might happen that they will not be in the same clade and by that I can find more information)?
代码示例为:
sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("A",25), rep("B",25), rep("C",25), rep("D",25))
## make unique rownames (equal rownames are not allowed)
rownames(sample) <- make.unique(groupCodes)
colorCodes <- c(A="red", B="green", C="blue", D="yellow")
## perform clustering
distSamples <- dist(sample)
hc <- hclust(distSamples)
## function to set label color
labelCol <- function(x) {
if (is.leaf(x)) {
## fetch label
label <- attr(x, "label")
code <- substr(label, 1, 1)
## use the following line to reset the label to one letter code
# attr(x, "label") <- code
attr(x, "nodePar") <- list(lab.col=colorCodes[code])
}
return(x)
}
## apply labelCol on all nodes of the dendrogram
d <- dendrapply(as.dendrogram(hc), labelCol)
plot(d)
推荐答案
看看?"plot.phylo"
:
library("ape")
plot(as.phylo(hc), tip.color=colorCodes[substr(rownames(sample), 1, 1)], type="fan")
这篇关于使用猿猴笼包装对R中的标签和彩色叶片树状图(系统发育)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!