2014-09-19 2 views
1

누구에게 도와 주시겠습니까?각 표본 이름에 Deprogram이 다른 색상으로 라벨을 붙이고 색상을 R

다음 코드를 사용하여 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

하여 오류의 원인은 당신이 숫자로 시작 "colorCodes"에 변수 이름을 사용하려고한다는 것입니다 (뭔가 R은 받아들이지 않는다).

> c(`1A_53`="red") 
1A_53 
"red" 

그러나 어떤 경우에도

, 나는 라벨을 착색, 그것은에서 labels_colors 기능을 사용하는 가장 쉬운 방법이 될 것이라고 생각 : 당신은 예를 들어, 특이한 이름을 포장 백 틱을 사용하여 우회 할 수 dendextend 패키지 예를 들어 : 패키지에 대한 자세한 내용은

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을 가질 수 있습니다.

enter image description here

관련 문제