r - 使用 ape 包在 R 中进行标签和色叶树状图(系统发育)

标签 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)

最佳答案

看看?“plot.phylo”:

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

enter image description here

关于r - 使用 ape 包在 R 中进行标签和色叶树状图(系统发育),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855631/

相关文章:

r - 在 R 中使用过滤器功能。需要为赛马数据库分配 NA 并保持数据集的长度相同

Selenium 问题

r - 使用 MICE 插补绘制适度回归中的交互项

python - 通过 scipy 给出相似矩阵的树状图

r - 如何标记切割树状图的终端节点?

r - 如何在R中将树转换为树状图?

r - 有效地复制 R 中的矩阵

matlab - 在 Matlab 图形中绘制图例

r - 当我改变轴位置 ggplot 停止轴标签旋转

r - 如何用大数据集绘制树状图?