I wrote that code quite a while ago, and it appears there's something changed a little in the mechanism.
The plot.dendrogram
function I used, has an argument nodePar
. The behaviour has changed since the last time I used that function, and although that's used normally for the inner nodes, it apparently has an effect on the outer nodes as well. The default value for pch
is 1:2
now, according to the help files.
Hence, you need to specifically specify pch=NA
in the attributes you add to the outer nodes in the colLab
function. Try adapting it like this:
colLab <- function(n)
{
if(is.leaf(n)) {
a <- attributes(n)
# clusMember - a vector designating leaf grouping
# labelColors - a vector of colors for the above grouping
labCol <- labelColors[clusMember[which(names(clusMember) == a$label)]]
attr(n, "nodePar") <-
if(is.list(a$nodePar)) c(a$nodePar, lab.col = labCol,pch=NA) else
list(lab.col = labCol,pch=NA)
}
n
}
On my machine, that solves the problem.
Alternatively, you could take a look at the argument use.edge.length
of the function plot.phylo
in the ape
package. You set it to FALSE
, but from your explanation I believe you want it to be set on the default, TRUE
.
EDIT: In order to make the function more generic, it might be a good idea to add labelColors
and clusMember
as arguments to the function. My quick-n-dirty solution isn't the best example of clean code...
Also forget what I said about using the edge length. the ape package interpretes it as a real dendrogram and putting use.edge.length
to TRUE
will convert the edge lengths to evolution time. Hence the 'weird' outlining of the dendrogram.
Also note that in case the treeleafs don't have a nodePar
attribute, adding extra parameters using the c()
function will lead to undesired effects: if you add eg lab.cex=0.6
, the c()
function will create a vector instead of a list, and convert the value for lab.cex
to character whenever there's a character value in the parameter list. In this case that's going to be the name of the color, and that explains the error you talk about in the comment.