label color in dendrograms

231 views
Skip to first unread message

Elise Tulloss

unread,
Jul 3, 2014, 6:52:18 PM7/3/14
to davi...@googlegroups.com

Hello,

This is my first question to the r users’ group.  Thanks in advance for any help.

 

I am trying to color the labels on a dendrogram I made using hclust.  I want to color the branch labels by treatment.  The treatment information is contained within the row names and the rows are in order in the data table so that the first 10 rows should be blue, the second 10 purple, third orange, and fourth red.  I can’t seem to find an easy way to do this.  The examples I found online seem to either write a complicated “if” function that I don’t understand and/or convert the plot from hclust into an object from another package (that I also don’t understand) and work on it from there.

 

I have attached my script and csv file if anyone wants to take a look.  Please phrase any response as if you are talking to your 90 year old grandma and teaching her to use the email. 

 

Thanks!!

Elise

 

dendrogram label example.R
Plant Comm Year 2 N Plots.csv

Dmitry Grapov

unread,
Jul 9, 2014, 5:10:42 PM7/9/14
to davi...@googlegroups.com
Hi,

This is my first reply in this group :)

Working with the code and object you provided. Here is how you should be able to dynamically set the label colors:

mydata<-read.csv("Plant Comm Year 2 N Plots.csv", header=TRUE,row.names=1)
library(vegan)
mydata <- decostand(mydata, method = "total")
# check total abundance in each sample
apply(mydata, 1, sum)
# look at the transformed data
mydata[1:5, 1:5]
ls()
# calculate Bray-Curtis distance among samples
mydata.bc.dist <- vegdist(mydata, method = "bray")
# cluster communities using average-linkage algorithm
mydata.bc.clust <- hclust(mydata.bc.dist, method = "average")
# plot cluster diagram
plot(mydata.bc.clust, ylab = "Bray-Curtis dissimilarity",col=rainbow(3))

#-------------Additions-------------------
#define colors you would like
# best way to do this is starting from a column giving the treatments
treat<-c(rep(1,10),rep(2,10),rep(3,10),rep(4,10))
# set colors
mycolors<-c("blue","purple","orange","red")[factor(treat)] # using factor to subset is useful when treatments are out of order

## function to set label color
# see added color.key which you can define for your data
labelCol <- function(x,color.key) {
  if (is.leaf(x)) {
    ## fetch label
    label <- attr(x, "label") 
    ## set label color 
color<-unname(color.key[label,])
    attr(x, "nodePar") <- list(lab.col=color)
  }
  return(x)
}

## apply labelCol on all nodes of the dendrogram
color.key<-matrix(mycolors);rownames(color.key)<-rownames(mydata) # extraction is done on rownames,use make.unique() if not unique
d <- dendrapply(as.dendrogram(mydata.bc.clust), labelCol, color.key)

plot(d)


#------------end

Hope this helps.
-Dmitry
Reply all
Reply to author
Forward
0 new messages