# Re: [R] Finding an order for an hclust (dendrogram) object without intersections

From: Tal Galili <tal.galili_at_gmail.com>
Date: Sun, 13 Jun 2010 19:39:55 +0300

Thanks Charles.

In the meantime, I found out the following code does the trick. But I am wondering if:
1) I might have made a mistake in it somewhere

# -----------------------------------------

order.a.tree <- function(tree)
{
num.of.leafs <- length(tree\$order)
for(i in 2:(num.of.leafs-1))
{

tree\$order <- order( cutree(tree, k = i))  }
return(tree)
}

#Example:

a <- list() # initialize empty object
# define merging pattern:
# negative numbers are leaves,
# positive are merged clusters (defined by row number in \$merge)
a\$merge <- matrix(c(-1, -2,

```                  -3, -4,
1,  2,
```

-5,-6,
3,4), nc=2, byrow=TRUE )
a\$height <- c(1, 1.5, 3,4,4.5) # define merge heights a\$order <- c(1,4,2,3,6,5) # order of leaves(trivial if hand-entered)
a\$labels <- 1:6# LETTERS[1:4] # labels of leaves class(a) <- "hclust" # make it an hclust object par(mfrow = c(1,2))
```plot(a)                     # look at the result
```
plot(order.a.tree(a))

# -----------------------------------------

Best,
Tal

```----------------Contact
Details:-------------------------------------------------------
```
Contact me: Tal.Galili_at_gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
```> On Sun, 13 Jun 2010, Tal Galili wrote:
>
>  Hello all,
>>
>> I manually created an hclust object.
>> Now I am looking to reorder the leafs so they won't intersect with each
>> other, and would be happy for advises on how to do that.
>>
>
> Have a look at the code for hclust().
>
>
> If you can instead create an object like 'hcl', then pass the results to
> .Fortran("hcass2",...), you should be done.
>
> HTH,
>
> Chuck
>
>
>> Here is an example code:
>>
>>
>> #-------------------------------------
>> a <- list()  # initialize empty object
>> # define merging pattern:
>> #    negative numbers are leaves,
>> #    positive are merged clusters (defined by row number in \$merge)
>> a\$merge <- matrix(c(-1, -2,
>>                   -3, -4,
>>                    1,  2,
>> -5,-6,
>> 3,4), nc=2, byrow=TRUE )
>> a\$height <- c(1, 1.5, 3,4,4.5)    # define merge heights
>> a\$order <- c(1,4,2,3,6,5)              # order of leaves(trivial if
>> hand-entered)
>> a\$labels <- 1:6# LETTERS[1:4]    # labels of leaves
>> class(a) <- "hclust"        # make it an hclust object
>> plot(a)                     # look at the result
>>
>> #-------------------------------------
>>
>> A working order solution in this example would be 1:6.
>> The question is how can I find it.
>>
>>
>> Thanks,
>> Tal
>>
>>
>>
>>
>>
>>
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili_at_gmail.com |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>>
>> ----------------------------------------------------------------------------------------------
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
> Charles C. Berry                            (858) 534-2098
>                                            Dept of Family/Preventive
> Medicine
> E mailto:cberry_at_tajo.ucsd.edu               UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>
>
>

[[alternative HTML version deleted]]

______________________________________________
```
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 13 Jun 2010 - 16:44:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 13 Jun 2010 - 18:00:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.