有人可以帮助我吗?
我正在尝试使用以下代码使用DNA距离矩阵绘制树状图。一切似乎都很好,但我似乎不能让每个标本都有不同的颜色; 我的距离矩阵的文件位于以下链接:
https://gist.github.com/plxsas/f02cd17e804f9fe1fe4a
T6 <- fasta2DNAbin(file="T6.fas", quiet=FALSE, snpOnly=FALSE)
dis <- dist.dna(T6, as.matrix = TRUE)
dis2 <- matrix(dis, nr=70,nc=70)
groupCodes <- c(rep("1A_53",6), rep("1A_56",5), rep("1A_57",6), rep("1A_59",6), rep("1A_63",5),
rep("1A_64",6), rep("1A_69",6), rep("1A_70",6), rep("1A_71",6),rep("1A_72",5),
rep("5A_15",6), rep("5A_32",7))
rownames(dis2) <- make.unique(groupCodes)
colorCodes <- c(1A_53="red", 1A_56="green", 1A_57="blue", 1A_59="yellow", 1A_63="darkgoldenrod1",
1A_64="burlywood3",1A_69="darkgray",1A_70="darkolivegreen",1A_71="darkorchid4",1A_72="darkkhaki",
5A_15="gray2",5A_32="darkseagreen2")
But I do get this error after this code:
Error: unexpected symbol in "colorCodes <- c(1A_53"
## perform clustering
hc <- hclust(as.dist(dis2))
## 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(as.phylo(hc), cex = 0.9, label.offset = 1)
答案 0 :(得分:0)
您的错误原因是您尝试在&#34; colorCodes&#34;中使用变量名称。以数字开头(R不接受的东西)。您可以通过使用反向标记来包围不寻常的名称来绕过它,例如:
> c(`1A_53`="red")
1A_53
"red"
但无论如何,我认为对于着色标签,最容易使用dendextend包中的labels_colors
函数。例如:
hc <- hclust(dist(USArrests[1:3,]), "ave")
dend <- as.dendrogram(hc)
# Defaults:
labels_colors(dend)
# NULL
plot(dend)
# let's add some color:
labels_colors(dend) <- 2:4
labels_colors(dend)
# Arizona Alabama Alaska
# 2 3 4
plot(dend)
有关该软件包的更多详细信息,您可以查看at its vignette。