Learn R Programming

phylotools (version 0.1.2)

hcreorder: Reorder the height for hclust object

Description

This function could be used to reorder the height values in hclust objects, especially when the hclust object was generated by as.hclust.phylo() in package ape. Since the method rect.hclust() or cutree() could not be applied to the hclust objected generated by ape before the height of the nodes be sorted in a increase order.

Usage

hcreorder(x)

Arguments

x
object in class hclust.

Value

hclust object with heights been reordered

Details

It should be note that if the height of the nodes are not sorted in increasing order, the rect.hclust() or cutree() will give wrong results. If the hclust object is generated by hclust() on distance matrix, there is no need to call this function. However, as hclust object was specified manually, or if generated using ape from a phylogenetic tree using as.hclust.phylo, you may have to call hcreorder first to reorder the heights of nodes. It should be noted that the merge matrix in hclust has also been regenerated according the definition of hclust object.

References

None.

See Also

hclust,cutree,rect.hclust

Examples

Run this code

### help file for hreorder()
### load ape library

library(ape)
data(bird.orders)
### convert the bird.orders to class hclust
bird.hclust <- as.hclust(multi2di(bird.orders))

### ready to draw tree/dendrograms
par(mfrow = c(1,3))

### plot phylogenetic tress
plot(bird.orders, direction = "downwards", cex = 1.5)


### the wrong results, because of the unordered height in hclust
### plot the bird.hclust
plot(bird.hclust, hang = -1 )

### add rectangles to the plot 
rect.hclust(bird.hclust, k = 4)


### the proper results.

### reorder the hclust object
bird.hclust.ordered <- hcreorder(bird.hclust)
### plot the reordered hclust object
plot(bird.hclust.ordered, hang = -1 )

### add rectangles to the plot 
rect.hclust(bird.hclust.ordered, k = 4)


### call cutree to divide the taxon to groups/clades 
cutree(bird.hclust.ordered, k = 4)

## Not run

Run the code above in your browser using DataLab