| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/rect.dendrogram.R |
| 3 |
\name{rect.dendrogram} |
| 4 |
\alias{rect.dendrogram} |
| 5 |
\title{Draw Rectangles Around a Dendrogram's Clusters} |
| 6 |
\source{ |
| 7 |
This function is based on \link{rect.hclust}, with slight modifications |
| 8 |
to have it work with a dendrogram, as well as a few added features |
| 9 |
(e.g: ... to rect, and horiz) |
| 10 |
|
| 11 |
The idea of adding text and shading lines under the clusters comes from |
| 12 |
skullkey from here: |
| 13 |
\url{https://stackoverflow.com/questions/4720307/change-dendrogram-leaves} |
| 14 |
} |
| 15 |
\usage{ |
| 16 |
rect.dendrogram( |
| 17 |
tree, |
| 18 |
k = NULL, |
| 19 |
which = NULL, |
| 20 |
x = NULL, |
| 21 |
h = NULL, |
| 22 |
border = 2, |
| 23 |
cluster = NULL, |
| 24 |
horiz = FALSE, |
| 25 |
density = NULL, |
| 26 |
angle = 45, |
| 27 |
text = NULL, |
| 28 |
text_cex = 1, |
| 29 |
text_col = 1, |
| 30 |
xpd = TRUE, |
| 31 |
lower_rect, |
| 32 |
upper_rect = 0, |
| 33 |
prop_k_height = 0.5, |
| 34 |
stop_if_out = FALSE, |
| 35 |
... |
| 36 |
) |
| 37 |
} |
| 38 |
\arguments{ |
| 39 |
\item{tree}{a \link{dendrogram} object.} |
| 40 |
|
| 41 |
\item{k}{Scalar. Cut the dendrogram such that exactly k clusters (if possible) are produced.} |
| 42 |
|
| 43 |
\item{which}{A vector selecting the clusters around |
| 44 |
which a rectangle should be drawn. which selects clusters by number |
| 45 |
(from left to right in the tree), Default is which = 1:k.} |
| 46 |
|
| 47 |
\item{x}{A vector selecting the clusters around |
| 48 |
which a rectangle should be drawn. x selects clusters |
| 49 |
containing the respective horizontal coordinates.} |
| 50 |
|
| 51 |
\item{h}{Scalar. Cut the dendrogram by cutting at height h. (k overrides h)} |
| 52 |
|
| 53 |
\item{border}{Vector with border colors for the rectangles.} |
| 54 |
|
| 55 |
\item{cluster}{Optional vector with cluster memberships as returned by |
| 56 |
cutree(dend_obj, k = k), can be specified for efficiency if already computed.} |
| 57 |
|
| 58 |
\item{horiz}{logical (FALSE), indicating if the rectangles |
| 59 |
should be drawn horizontally or not (for when using |
| 60 |
plot(dend, horiz = TRUE) ) .} |
| 61 |
|
| 62 |
\item{density}{Passed to \link{rect}: the density of shading lines, |
| 63 |
in lines per inch. The default value of NULL means that |
| 64 |
no shading lines are drawn. A zero value of density means |
| 65 |
no shading lines whereas negative values (and NA) |
| 66 |
suppress shading (and so allow color filling). |
| 67 |
If border is a vector of colors, the color of density will default to 1.} |
| 68 |
|
| 69 |
\item{angle}{Passed to \link{rect}: angle (in degrees) of the shading lines. |
| 70 |
(default is 45)} |
| 71 |
|
| 72 |
\item{text}{a character vector of labels to plot underneath the clusters. |
| 73 |
When NULL (default), no text is displayed.} |
| 74 |
|
| 75 |
\item{text_cex}{a numeric (scalar) value of the text's cex value.} |
| 76 |
|
| 77 |
\item{text_col}{a (scalar) value of the text's col(or) value.} |
| 78 |
|
| 79 |
\item{xpd}{A logical value (or NA.), passed to \link{par}. |
| 80 |
Default is TRUE, in order to allow the rect to be below the labels. |
| 81 |
If FALSE, all plotting is clipped to the plot region, if TRUE, |
| 82 |
all plotting is clipped to the figure region, and if NA, all plotting |
| 83 |
is clipped to the device region. See also \link{clip}.} |
| 84 |
|
| 85 |
\item{lower_rect}{a (scalar) value of how low should the lower part of the rect be. |
| 86 |
If missing, it will take the value of par("usr")[3L] (or par("usr")[2L], depending |
| 87 |
if horiz = TRUE or not), with also the width of the labels. (notice that we |
| 88 |
would like to keep xpd = TRUE if we want the rect to be after the labels!) |
| 89 |
You can use a value such as 0, to get the rect above the labels. |
| 90 |
|
| 91 |
Notice that for a plot with small margins, it would be better to set this |
| 92 |
parameter manually.} |
| 93 |
|
| 94 |
\item{upper_rect}{a (scalar) value to add (default is 0) to how high should the upper part of the rect be.} |
| 95 |
|
| 96 |
\item{prop_k_height}{a (scalar) value (should be between 0 to 1), indicating what proportion |
| 97 |
of the height our rect will be between the height needed for k and k+1 clustering.} |
| 98 |
|
| 99 |
\item{stop_if_out}{logical (default is TRUE). This makes the function |
| 100 |
stop if k of the locator is outside the range (this default reproduces the behavior |
| 101 |
of the rect.hclust function).} |
| 102 |
|
| 103 |
\item{...}{parameters passed to rect (such as lwd, lty, etc.)} |
| 104 |
} |
| 105 |
\value{ |
| 106 |
(Invisibly) returns a list where each element contains a vector |
| 107 |
of data points contained in the respective cluster. |
| 108 |
} |
| 109 |
\description{ |
| 110 |
Draws rectangles around the branches of a dendrogram |
| 111 |
highlighting the corresponding clusters. |
| 112 |
First the dendrogram is cut at a certain level, |
| 113 |
then a rectangle is drawn around selected branches. |
| 114 |
} |
| 115 |
\examples{ |
| 116 |
|
| 117 |
set.seed(23235) |
| 118 |
ss <- sample(1:150, 10) |
| 119 |
hc <- iris[ss, -5] \%>\% |
| 120 |
dist() \%>\% |
| 121 |
hclust() |
| 122 |
dend <- hc \%>\% as.dendrogram() |
| 123 |
|
| 124 |
plot(dend) |
| 125 |
rect.dendrogram(dend, 2, border = 2) |
| 126 |
rect.dendrogram(dend, 3, border = 4) |
| 127 |
Vectorize(rect.dendrogram, "k")(dend, 4:5, border = 6) |
| 128 |
|
| 129 |
plot(dend) |
| 130 |
rect.dendrogram(dend, 3, |
| 131 |
border = 1:3, |
| 132 |
density = 2, text = c("1", "b", "miao"), text_cex = 3 |
| 133 |
) |
| 134 |
|
| 135 |
plot(dend) |
| 136 |
rect.dendrogram(dend, 4, which = c(1, 3), border = c(2, 3)) |
| 137 |
rect.dendrogram(dend, 4, x = 5, border = c(4)) |
| 138 |
rect.dendrogram(dend, 3, border = 3, lwd = 2, lty = 2) |
| 139 |
# now THIS, you can not do with the old rect.hclust |
| 140 |
plot(dend, horiz = TRUE) |
| 141 |
rect.dendrogram(dend, 2, border = 2, horiz = TRUE) |
| 142 |
rect.dendrogram(dend, 4, border = 4, lty = 2, lwd = 3, horiz = TRUE) |
| 143 |
|
| 144 |
# This had previously failed since it worked with a wrong k. |
| 145 |
|
| 146 |
dend15 <- c(1:5) \%>\% |
| 147 |
dist() \%>\% |
| 148 |
hclust(method = "average") \%>\% |
| 149 |
as.dendrogram() |
| 150 |
# dend15 <- c(1:25) \%>\% dist \%>\% hclust(method = "average") \%>\% as.dendrogram |
| 151 |
dend15 \%>\% |
| 152 |
set("branches_k_color") \%>\% |
| 153 |
plot() |
| 154 |
dend15 \%>\% rect.dendrogram( |
| 155 |
k = 3, |
| 156 |
border = 8, lty = 5, lwd = 2 |
| 157 |
) |
| 158 |
} |
| 159 |
\seealso{ |
| 160 |
\link{rect.hclust}, \link{order.dendrogram}, \link{cutree.dendrogram} |
| 161 |
} |