标签和颜色叶树状图 [英] Label and color leaf dendrogram
问题描述
我正在尝试创建一个树状图,我的样本是否有 5 个组代码(充当样本名称/物种/等,但它是重复的).
I am trying to create a dendrogram, were my samples have 5 group codes (act as sample name/species/etc but its repetitive).
因此,我有两个问题需要帮助:
Therefore, I have two issues that a help will be great:
如何在叶子标签(而不是样本编号)中显示组代码?
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)?
是否可以使用我的脚本(ape 或 ggdendro)这样做:
Is it possible to do so with my script to do so (ape or ggdendro):
sample<-read.table("C:/.../DOutput.txt", header=F, sep="")
groupCodes <- sample[,1]
sample2<-sample[,2:100]
d <- dist(sample2, method = "euclidean")
fit <- hclust(d, method="ward")
plot(as.phylo(fit), type="fan")
ggdendrogram(fit, theme_dendro=FALSE)
替换我的 read.table 的随机数据框:
A random dataframe to replace my read.table:
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)) # fixed error
sample2 <- data.frame(cbind(groupCodes), sample)
推荐答案
这里有一个解决方案,使用名为dendextend",正是为这种事情而构建的.
Here is a solution for this question using a new package called "dendextend", built exactly for this sort of thing.
您可以在包的演示文稿和小插图中看到许多示例,在以下 URL 的使用"部分:https://github.com/talgalili/dendextend
You can see many examples in the presentations and vignettes of the package, in the "usage" section in the following URL: https://github.com/talgalili/dendextend
这是这个问题的解决方案:(注意如何重新排序颜色以首先适合数据,然后适合树状图的新顺序的重要性)
Here is the solution for this question: (notice the importance of how to re-order the colors to first fit the data, and then to fit the new order of the dendrogram)
####################
## Getting the data:
sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("Cont",25), rep("Tre1",25), rep("Tre2",25), rep("Tre3",25))
rownames(sample) <- make.unique(groupCodes)
colorCodes <- c(Cont="red", Tre1="green", Tre2="blue", Tre3="yellow")
distSamples <- dist(sample)
hc <- hclust(distSamples)
dend <- as.dendrogram(hc)
####################
## installing dendextend for the first time:
install.packages('dendextend')
####################
## Solving the question:
# loading the package
library(dendextend)
# Assigning the labels of dendrogram object with new colors:
labels_colors(dend) <- colorCodes[groupCodes][order.dendrogram(dend)]
# Plotting the new dendrogram
plot(dend)
####################
## A sub tree - so we can see better what we got:
par(cex = 1)
plot(dend[[1]], horiz = TRUE)
这篇关于标签和颜色叶树状图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!