1

Can someone help me, please?

I am trying to draw dendrogram using DNA distance matrix using the following code. Everything is seem fine but I do not seem to be able to have each specimens with different color; The file of my distance matrix is in the following link:

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)
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
user48386
  • 95
  • 1
  • 6

1 Answers1

0

The reason for your error is that you are trying to use variable names in "colorCodes" which begins with a number (something which R does not accept). You could bypass it by using the back-tick to wrap the unusual name, for example:

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

But in any case, I think that for coloring your labels, it would be the easiest to use the labels_colors function from the dendextend package. For example:

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)

For more details on the package, you can have a look at its vignette.

enter image description here

Tal Galili
  • 24,605
  • 44
  • 129
  • 187