Hello, I am a new R user and have a question regarding dendrogram coloring. I would like to color each leaf in the dendrogram (dhc) according to a specific criterion. For me this criterion is the gene name. For this, I created a data.frame with 2 variables: The gene name and the corresponding color. Using the following function, adapted from "dendrapply {stats}", I still have the same color for the leaves. Using the standard function (with the my_colors[i]), I do have the leaves of different colors !!! The structure of "my_colors[i]" is the same as the structure of "color" !!! Do you have any idea on what is going wrong ? Many thanks for your help, targetGenes<-length(unique(bm$Target)) #Getting the number of unique target genes my_colors<-rainbow(targetGenes) #Creating a vector of colors for each gene gene2color<-data.frame(gene=unique(bm$Target), color=my_colors) #Creating the data.frame dhc<-as.dendrogram(fit.x) #the dendrogram using the hclust object (fit.x) local({ colLab <<- function(n) { if(is.leaf(n)) { a <- attributes(n) crm <- as.numeric(a$label) gene <- as.character(bm[which(bm$CRM_ID==crm),5]) color <- as.character(gene2color[which(gene2color$genes==gene),2]) i <<- i+1 attr(n, "nodePar") <- # c(a$nodePar, list(lab.col = my_colors[i], lab.font= i%%3)) c(a$nodePar, list(lab.col = color)) } n } my_colors <- grDevices::rainbow(attr(dhc,"members")) i <- 0 }) dL <- dendrapply(dhc, colLab) op <- par(mfrow=2:1) plot(dhc) plot(dL)pierre.khoueiry at embl.de -- View this message in context: http://n4.nabble.com/Coloring-leaves-in-Dendrogram-according-to-gene-names-tp1838329p1838329.html Sent from the R help mailing list archive at Nabble.com.