I am trying to build the result of agglomerative clustering (UPGMA with Agnes) in the same style as when building a tree using the ape package. A simple example that I include in the image below 
The key problem is that I want to be able to color the leaves of the dendrogram based on the template in the leaf labels. I tried two approaches: either I used hc2Newick , or I used the code of Joris Mayes, as suggested in response to Change Dendrogram leaves . Both did not give a satisfactory result. Perhaps I do not quite understand how the dendrograms work. Saving the ASCII object abundance.agnes.ave (saved from the agnes launch) can be found at https://www.dropbox.com/s/gke9qnvwptltkky/abundance.agnes.ave .
When I use the first option (with hc2Newick from the bioconductor ctc ), I get the following figure when using this code:
write(hc2Newick(as.hclust(abundance.agnes.ave)),file="all_samples_euclidean.tre") eucltree<-read.tree(file="all_samples_euclidean.tre") eucltree.laz<-ladderize(eucltree,FALSE) tiplabs<-eucltree$tip.label numbertiplabs<-length(tiplabs) colourtips<-rep("green",numbertiplabs) colourtips[grep("II",tiplabs)]<-"red" plot(eucltree.laz,tip.color=colourtips,adj=1,cex=0.6,use.edge.length=F) add.scale.bar()

This is obviously not perfect, the “alignment” of the plot is not the way I wanted. I believe this is related to calculating the length of the branches, but I have no vague idea how to solve this problem. Of course, compared to the results of the colLab function, which are more reminiscent of the dendrogram style that I would like to report. Also, using use.edge.length=T in the above code gives clustering that is not correctly aligned: 
The second approach using the colLab function of Joris Meys with the following code yields the following digit
clusDendro<-as.dendrogram(as.hclust(abundance.agnes.ave)) labelColors<-c("red","green") clusMember<-rep(1,length(rownames(abundance.x))) clusMember[grep("II",rownames(abundance.x))]<-2 names(clusMember)<-rownames(abundance.x) colLab <- function(n) { if(is.leaf(n)) { a <- attributes(n)
This plot is close to what I want, but I don’t know why open circles appear on the leaves and how to remove them.
Any help is greatly appreciated.
Yours faithfully,
FM