[R] Color individual leaf labels in dendrogram

Kennedy henrik.aldberg at gmail.com
Sat Oct 16 16:31:48 CEST 2010


Thank you Brian,

Your suggestion helped me a bit but I am not quite there yet. Now I have the
following code:

  library(cluster) 
  library(stats)

  D<-matrix(nr=4,nc=4)
  rownames(D)<-c("Node 1","Node 2","Node 3","Node 4")
  D[1,]<-c(0,.6,.1,.7)
  D[2,]<-c(.6,0,.3,.9)
  D[3,]<-c(.1,.3,0,.9)
  D[4,]<-c(.7,.9,.9,0)

  C<-agnes(D,diss=T,method="complete")

  DC<-as.dendrogram(C) 
  # Function for coloring labels
  colLab <<- function(n){ 
    if(is.leaf(n)){ 
       a <- attributes(n) 
       i <<- i+1 
       attr(n, "nodePar") <- c(a$nodePar, list(lab.col = mycol[i])) 
    } 
    n 
  } 
  # Define vector with label colors
  mycol<-vector(length=attributes(DC)$members)
  for(i in 1:attributes(DC)$members){
	  mycol[i]<-"black"
	  }
  mycol[3]<-"red"
 
  i <- 0 

  dL <- dendrapply(DC, colLab) 
  plot(dL) 


The remaining problems are:
1. I want to get rid of the little circles that are drawn at the end of each
branch.

2. How do I access the original nodes. E.g. in the example above I set
mycol[3]<-"red" which affects the third node in the tree which is Node 2. My
intention was to paint the label "Node 3" in red. When the tree is larger
this becomes a problem.


Sincerely

Henrik


-- 
View this message in context: http://r.789695.n4.nabble.com/Color-individual-leaf-labels-in-dendrogram-tp2996982p2998322.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list