o.k,

I found a working solution for this.
If anyone in the future will face the problem, here is something that works
(Although it is probably not the best solution out there)

Best,
Tal


#------------------------------------------------------------------
problematic.tree <-
structure(list(merge = structure(c(-3, -24, 1, -25, 4, -27, 5,
 -22, 8, 3, 9, -5, 10, 11, 12, 6, 14, 7, 18, 19, 20, 21, 22, 23,
24, 25, -13, -23, -14, -21, -20, -26, -19, -1, 2, -15, -7, -4,
 -9, -18, -6, -17, -12, 17, -2, -8, -10, -16, 13, 15, 16, -11), .Dim =
c(26L,
2L)), height = c(0.0833333333333333, 0.0866666666666667, 0.116666666666667,
 0.136507936507937, 0.220634920634921, 0.622222222222222,
0.674603174603175,
0.823333333333333, 1.06349206349206, 1.27698412698413, 1.37222222222222,
 2.00952380952381, 2.2975, 2.38888888888889, 2.68666666666667,
2.9, 3.14736842105263, 3.55634920634921, 3.7921768707483, 3.84183673469388,
 3.93817373103087, 4.54464285714286, 4.81438464274599, 5.10895156778615,
5.36142237562854, 6.19122779197967), order = c(1L, 2L, 3L, 4L,
 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 11L), labels = 1:27), .Names =
c("merge",
 "height", "order", "labels"), class = "hclust")
plot(problematic.tree )

order.a.tree <- function(tree)
{
num.of.leafs <- length(tree$order)
 matrix.to.order <- NULL
 for(i in 1:(num.of.leafs))
{
 matrix.to.order <- cbind(matrix.to.order ,  cutree(tree, k = i))
}
 id <- seq_len(dim(matrix.to.order)[1])
matrix.to.order <- data.frame(id, matrix.to.order)
 print(matrix.to.order)
require(doBy) # The ordering engine of this function is based on orderBy
from the doBy package
 ordered.matrix <- eval(parse(text = paste("orderBy(~",
paste(colnames(matrix.to.order)[-1], collapse = "+"),   ",
data=matrix.to.order)")))
 print(ordered.matrix)
 tree$order <- (ordered.matrix$id)
 return(tree)
}
plot(order.a.tree(problematic.tree))
#------------------------------------------------------------------



----------------Contact
Details:-------------------------------------------------------
Contact me: Tal.Galili@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Sun, Jun 13, 2010 at 8:42 PM, Tal Galili <tal.galili@gmail.com> wrote:

> o.k,
> I found an example where my algorithm can't fix the tree order.
> But I don't know how to resolve it.
>
> Here is the code to reproduce the problem:
>
>
>
> # ----------------------------------------------------
>
> order.a.tree <- function(tree)
> {
> num.of.leafs <- length(tree$order)
>
> for(i in 1:(num.of.leafs))
> {
>  tree$order <-  order( cutree(tree, k = i))
> }
>  return(tree)
> }
>
> problematic.tree <-
> structure(list(merge = structure(c(-3, -24, 1, -25, 4, -27, 5,
>  -22, 8, 3, 9, -5, 10, 11, 12, 6, 14, 7, 18, 19, 20, 21, 22, 23,
> 24, 25, -13, -23, -14, -21, -20, -26, -19, -1, 2, -15, -7, -4,
>  -9, -18, -6, -17, -12, 17, -2, -8, -10, -16, 13, 15, 16, -11), .Dim =
> c(26L,
> 2L)), height = c(0.0833333333333333, 0.0866666666666667,
> 0.116666666666667,
>  0.136507936507937, 0.220634920634921, 0.622222222222222,
> 0.674603174603175,
> 0.823333333333333, 1.06349206349206, 1.27698412698413, 1.37222222222222,
>  2.00952380952381, 2.2975, 2.38888888888889, 2.68666666666667,
> 2.9, 3.14736842105263, 3.55634920634921, 3.7921768707483,
> 3.84183673469388,
>  3.93817373103087, 4.54464285714286, 4.81438464274599, 5.10895156778615,
> 5.36142237562854, 6.19122779197967), order = c(1L, 2L, 3L, 4L,
>  5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
> 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 11L), labels = 1:27), .Names =
> c("merge",
>  "height", "order", "labels"), class = "hclust")
> plot(order.a.tree(problematic.tree ))
>
> # ----------------------------------------------------
>
>
> Thanks,
> Tal
>
>
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili@gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
>
> ----------------------------------------------------------------------------------------------
>
>
>
>
> On Sun, Jun 13, 2010 at 7:39 PM, Tal Galili <tal.galili@gmail.com> wrote:
>
>> 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
>> 2) If there are other (smarter) ways of going about this.
>>
>> Here is the solution I wrote:
>>
>>
>>
>> # -----------------------------------------
>>
>> 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))
>>
>> # -----------------------------------------
>>
>>
>>
>> Any comments, or other solutions will be very welcomed.
>>
>> Best,
>> Tal
>>
>>
>>
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili@gmail.com |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>>
>> ----------------------------------------------------------------------------------------------
>>
>>
>>
>>
>> On Sun, Jun 13, 2010 at 7:08 PM, Charles C. Berry <cberry@tajo.ucsd.edu>wrote:
>>
>>> 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@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@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.
>>>>
>>>>
>>> Charles C. Berry                            (858) 534-2098
>>>                                            Dept of Family/Preventive
>>> Medicine
>>> E mailto:cberry@tajo.ucsd.edu               UC San Diego
>>> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
>>> 92093-0901
>>>
>>>
>>>
>>
>

	[[alternative HTML version deleted]]

