Debian Codegraph @

man/rotate.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/rotate.R
3 \name{rotate}
4 \alias{rotate}
5 \alias{rotate.default}
6 \alias{rotate.dendrogram}
7 \alias{rotate.hclust}
8 \alias{rotate.phylo}
9 \alias{sort.dendrogram}
10 \alias{sort.hclust}
11 \alias{sort.dendlist}
12 \alias{rev.hclust}
13 \title{Rotate a tree object}
14 \usage{
15 rotate(x, ...)
16
17 \method{rotate}{default}(x, order, ...)
18
19 \method{rotate}{dendrogram}(x, order, ...)
20
21 \method{rotate}{hclust}(x, order, ...)
22
23 \method{rotate}{phylo}(x, ..., phy)
24
25 \method{sort}{dendrogram}(x, decreasing = FALSE, type = c("labels", "nodes"), ...)
26
27 \method{sort}{hclust}(x, decreasing = FALSE, ...)
28
29 \method{sort}{dendlist}(x, ...)
30
31 \method{rev}{hclust}(x, ...)
32 }
33 \arguments{
34 \item{x}{a tree object (either a \code{dendrogram} or \code{hclust})}
35
36 \item{...}{parameters passed (for example, in case of sort)}
37
38 \item{order}{Either numeric or character vector.
39 Is numeric: it is a numeric vector with the order of the value to be
40 assigned to object's label. The numbers say are just like when you use \link{order}:
41 which of the items on the tree-plot should be "first" (e.g: most left),
42 second etc. (this is relevant only to \code{rotate})
43 Is character: it must be a vector with the content of labels(x), in the
44 order we'd like to have the new tree.}
45
46 \item{phy}{a placeholder in case the user uses "phy ="}
47
48 \item{decreasing}{logical. Should the sort be increasing or decreasing? Not available for partial sorting. (relevant only to \code{sort})}
49
50 \item{type}{a character indicating how to sort. If "labels" then by lexicographic
51 order of the labels. If "nodes", then by using \link{ladderize} (order so that
52 recursively, the leftmost branch will be the smallest)}
53 }
54 \value{
55 A rotated tree object
56 }
57 \description{
58 Rotates, rev and sort the branches of a tree object (dendrogram, hclust)
59 based on a vector - either of labels order (numbers) or the labels in their
60 new order (character).
61 }
62 \details{
63 The motivation for this function came from the function
64 \code{\link{order.dendrogram}} NOT being very intuitive.
65 What \code{rotate} aims to do is give a simple tree rotation function which
66 is based on the order which the user would like to see the tree rotated by
67 (just as \code{\link{order}} works for numeric vectors).
68
69 \code{\link{rev.dendrogram}} is part of base R, and returns the tree object
70 after rotating it so that the order of the labels is reversed.
71 Here we added an S3 method for hclust objects.
72
73 The \code{sort} methods sort the labels of the tree (using \code{order})
74 and then attempts to rotate the tree to fit that order.
75
76 The hclust method of "\code{rotate}" works by first changing the object into
77 dendrogram, performing the rotation, and then changing it back to hclust.
78 Special care is taken in preserving some of the properties of the hclust
79 object.
80
81 The ape package has its own \code{\link[ape]{rotate}} function (which is sadly not S3, so cannot be easily connected with the
82 current implementation). Still, there is an S3 plug that makes sure people
83 loading ape first and then dendextend will still be able to
84 use \code{rotate} without a problem.
85 Notice that if you first load ape and only then dendextend,
86 using "rotate" will fail with the error: "Error in rotate(dend, ____) :
87 object 'phy' is not of class 'phylo'" - this is because rotate in ape
88 is not S3 and will fail to find the rotate.dendrogram function.
89 In such a case, simply run \code{unloadNamespace(ape)}. Or, you can run:
90 \code{unloadNamespace("dendextend"); attachNamespace("dendextend")}
91 The solution for this is that if you have ape installed on your machine,
92 it will be loaded when you load dendextend (but after it).
93 This way, \code{rotate} will work fine for both dendrogram and phylo
94 objects.
95 }
96 \examples{
97 hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
98 dend <- as.dendrogram(hc)
99
100 # For dendrogram objects:
101 labels_colors(dend) <- rainbow(nleaves(dend))
102 # let's color the labels to make the followup of the rotation easier
103 par(mfrow = c(1, 2))
104 plot(dend, main = "Original tree")
105 plot(rotate(dend, c(2:5, 1)),
106 main =
107 "Rotates the left most leaf \n into the right side of the tree"
108 )
109 par(mfrow = c(1, 2))
110 plot(dend, main = "Original tree")
111 plot(sort(dend), main = "Sorts the labels by alphabetical order \n
112 and rotates the tree to give the best fit possible")
113 par(mfrow = c(1, 2))
114 plot(dend, main = "Original tree")
115 plot(rev(dend), main = "Reverses the order of the tree labels")
116
117 # For hclust objects:
118 plot(hc)
119 plot(rotate(hc, c(2:5, 1)), main = "Rotates the left most leaf \n
120 into the right side of the tree")
121
122 par(mfrow = c(1, 3))
123 dend \%>\% plot(main = "Original tree")
124 dend \%>\%
125 sort() \%>\%
126 plot(main = "labels sort")
127 dend \%>\%
128 sort(type = "nodes") \%>\%
129 plot(main = "nodes (ladderize) sort")
130 }
131 \seealso{
132 \code{\link{order.dendrogram}}, \code{\link{order}},
133 \code{\link{rev.dendrogram}}, \code{\link[ape]{rotate}}, \link{ladderize}
134 }