Rotates, rev and sort the branches of a tree object (dendrogram, hclust) based on a vector - eithor of labels order (numbers) or the labels in their new order (character).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
rotate(x, ...) ## Default S3 method: rotate(x, order, ...) ## S3 method for class 'dendrogram' rotate(x, order, ...) ## S3 method for class 'hclust' rotate(x, order, ...) ## S3 method for class 'phylo' rotate(x, ..., phy) ## S3 method for class 'dendrogram' sort(x, decreasing = FALSE, type = c("labels", "nodes"), ...) ## S3 method for class 'hclust' sort(x, decreasing = FALSE, ...) ## S3 method for class 'dendlist' sort(x, ...) ## S3 method for class 'hclust' rev(x, ...)
a tree object (either a
parameters passed (for example, in case of sort)
Either numeric or character vector.
Is numeric: it is a numeric vector with the order of the value to be
assigned to object's label. The numbers say are just like when you use order:
which of the items on the tree-plot should be "first" (e.g: most left),
second etc. (this is relevant only to
a placeholder in case the user uses "phy ="
logical. Should the sort be increasing or decreasing? Not available for partial sorting. (relevant only to
a character indicating how to sort. If "labels" then by lexicographic order of the labels. If "nodes", then by using ladderize (order so that recursively, the leftmost branch will be the smallest)
The motivation for this function came from the function
order.dendrogram NOT being very intuitive.
rotate aims to do is give a simple tree rotation function which
is based on the order which the user would like to see the tree rotated by
order works for numeric vectors).
rev.dendrogram is part of base R, and returns the tree object
after rotating it so that the order of the labels is reversed.
Here we added an S3 method for hclust objects.
sort methods sort the labels of the tree (using
and then attempts to rotate the tree to fit that order.
The hclust method of "
rotate" works by first changing the object into
dendrogram, performing the rotation, and then changing it back to hclust.
Special care is taken in preserving some of the properties of the hclust
The ape package has its own
(Which is sadly not S3, so cannot be easily connected with the
current implementation). Still, there is an S3 plug that makes sure people
loading first ape and then dendextend will still be able to
rotate without a problem.
Notice that if you will first load ape and only then dendextend,
using "rotate" will fail with the error: "Error in rotate(dend, ____) :
object "phy" is not of class "phylo"" - this is because rotate in ape
is not S3 and will fail to find the rotate.dendrogram function.
In such a case simply run
unloadNamespace(ape). Or, you can run:
The solution for this is that if you have ape installed on your machine,
It will be loaded when you load dendextend (but after it).
rotate will work fine for both dendrogram AND phylo
A rotated tree object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
hc <- hclust(dist(USArrests[c(1,6,13,20, 23),]), "ave") dend <- as.dendrogram(hc) # For dendrogram objects: labels_colors(dend) <- rainbow(nleaves(dend)) # let's color the labels to make the followup of the rotation easier par(mfrow = c(1,2)) plot(dend, main = "Original tree") plot(rotate(dend, c(2:5,1)), main = "Rotates the left most leaf \n into the right side of the tree") par(mfrow = c(1,2)) plot(dend, main = "Original tree") plot(sort(dend), main = "Sorts the labels by alphabetical order \n and rotates the tree to give the best fit possible") par(mfrow = c(1,2)) plot(dend, main = "Original tree") plot(rev(dend), main = "Reverses the order of the tree labels") # For hclust objects: plot(hc) plot(rotate(hc, c(2:5,1)), main = "Rotates the left most leaf \n into the right side of the tree") par(mfrow = c(1,3)) dend %>% plot(main = "Original tree") dend %>% sort %>% plot(main = "labels sort") dend %>% sort(type = "nodes") %>% plot(main = "nodes (ladderize) sort")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.