1

有人能帮助我吗?

我正在尝试使用以下代码使用 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)
4

1 回答 1

0

您的错误的原因是您试图在“colorCodes”中使用以数字开头的变量名称(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)

有关包装的更多详细信息,您可以查看它的小插图

在此处输入图像描述

于 2014-09-21T14:32:10.857 回答