使用ape包的R中的标签和颜色叶树形图(系统发育)

时间:2013-09-17 16:57:26

标签: r plot dendrogram phylogeny dendextend

在上一篇文章(Label and color leaf dendrogram in r)之后,我有一个后续问题。

我的问题类似于提到的帖子,但我想知道是否可以使用ape(例如,plot(as.phylo(fit), type="fan", labelCol)来完成,因为它有更多类型的系统发育。

提到的帖子问题是:

  • 如何在叶标签中显示组代码(而不是样本编号)?

  • 我希望为每个代码组分配一种颜色并根据它对叶子标签着色(可能会发生它们不在同一个分支中,我可以找到更多信息)?

代码示例是:

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)

2 个答案:

答案 0 :(得分:2)

问题的另一个解决方案是使用组合两个包的新circlize_dendrogram函数:circlize和dendextend。您首先需要安装它们:

install.packages("circlize")
devtools::install_github('talgalili/dendextend')

以下是要运行的代码:

# YOUR CODE
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)

#--------------
# NEW CODE

dend <- as.dendrogram(hc )
library(dendextend)
labels_colors(dend) <- colorCodes 
# plot(dend)
dend <- color_branches(dend, k=4)

# plot the radial plot
par(mar = rep(0,4))
# circlize_dendrogram(dend, dend_track_height = 0.8) 
circlize_dendrogram(dend) 

这是由此产生的情节:

enter image description here

答案 1 :(得分:1)

查看?"plot.phylo"

library("ape")
plot(as.phylo(hc), tip.color=colorCodes[substr(rownames(sample), 1, 1)], type="fan")

enter image description here