[R] x/y coordinates of dendrogram branches
Romain Francois
francoisromain at free.fr
Wed Nov 2 16:05:05 CET 2005
Hi Timo,
Here is a quick and dirty recursive function.
##################
absi <- function(hc, level=length(hc$height), init=TRUE){
if(init){
.count <<- 0
.topAbsis <<- NULL
.heights <<- NULL
}
if(level<0) {
.count <<- .count + 1
return(.count)
}
node <- hc$merge[level,]
le <- absi(hc, node[1], init=FALSE)
ri <- absi(hc, node[2], init=FALSE)
mid <- (le+ri)/2
.topAbsis <<- c(.topAbsis, mid)
.heights <<- c(.heights, hc$height[level])
invisible(mid)
}
#################
R> mydata <- c(1,2,3,4,5)
R> hc <- hclust(dist(mydata), method="average")
R> absi(hc)
R> .topAbsis # those are x-coordinates
[1] 1.500 4.500 3.750 2.625
R> .heights # and y-coordinates
[1] 1.0 1.0 1.5 2.5
R> plot(hc)
R> points(.topAbsis, .heights, pch=21, bg=2, cex=5)
Is this close to what tou need ?
Romain
Le 02.11.2005 14:51, Timo Becker a écrit :
>Dear R-users,
>
>I need some help concerning the plotting of dendrograms for hierarchical
>agglomerative clustering.
>The agglomeration niveau of each step should be displayed at the
>branches of the dendrogram.
>For this I need the x/y coordinates of the branch-agglomerations of the
>dendrogram.
>The y-values are known (the heights of the agglomeration), but how can I
>get the x-values?
>
> > mydata <- c(1,2,3,4,5)
> > hc <- hclust(dist(mydata), method="average")
> > hc$height # these are the y-coordinates
>[1] 1.0 1.0 1.5 2.5
>
>I experimented with the dendrogram object because it gives the midpoints:
>
> > dend <- as.dendrogram(hc)
> > attributes(dend[[1]])$midpoint
>[1] 0.5
>
>Perhaps I could loop over all possible branches and get the midpoints if
>I knew how to convert them to x-values.
>The manual says that midpoint is the "numeric horizontal distance of the
>node from the left border (the leftmost leaf) of the
>branch (unit 1 between all leaves)."
>But how do I know which one is the leftmost leaf and what is its
>horizontal value?
>
>Thanks in advance,
>Timo
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
>
--
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
+---------------------------------------------------------------+
| Romain FRANCOIS - http://francoisromain.free.fr |
| Doctorant INRIA Futurs / EDF |
+---------------------------------------------------------------+
More information about the R-help
mailing list