| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/colored_bars.R |
| 3 |
\name{colored_bars} |
| 4 |
\alias{colored_bars} |
| 5 |
\title{Add colored bars to a dendrogram} |
| 6 |
\source{ |
| 7 |
This function is based on the \link[moduleColor]{plotHclustColors} from the |
| 8 |
{moduleColor} R package. It was modified so that it would |
| 9 |
work with dendrograms (and not just hclust objects), as well allow to |
| 10 |
add the colored bars on top of an existing plot (and not only as a seperate plot). |
| 11 |
|
| 12 |
See: \url{https://cran.r-project.org/package=moduleColor} |
| 13 |
For more details. |
| 14 |
} |
| 15 |
\usage{ |
| 16 |
colored_bars( |
| 17 |
colors, |
| 18 |
dend, |
| 19 |
rowLabels = NULL, |
| 20 |
cex.rowLabels = 0.9, |
| 21 |
add = TRUE, |
| 22 |
y_scale, |
| 23 |
y_shift, |
| 24 |
text_shift = 1, |
| 25 |
sort_by_labels_order = TRUE, |
| 26 |
horiz = FALSE, |
| 27 |
... |
| 28 |
) |
| 29 |
} |
| 30 |
\arguments{ |
| 31 |
\item{colors}{Coloring of objects on the dendrogram. Either a vector (one color per object) |
| 32 |
or a matrix (can also be an array or a data frame) |
| 33 |
with each column giving one group with color per object. |
| 34 |
Each column will be plotted as a horizontal row of colors (when horiz = FALSE) |
| 35 |
under the dendrogram. |
| 36 |
As long as the sort_by_labels_order paramter is TRUE (default), the colors vector/matrix should |
| 37 |
be provided in the order of the original data order (and it will be re-ordered automaticall to |
| 38 |
the order of the dendrogram)} |
| 39 |
|
| 40 |
\item{dend}{a dendrogram object. If missing, the colors are plotted without and re-ordering |
| 41 |
(this assumes that the colors are already ordered based on the dend's labels) |
| 42 |
This is also important in order to get the correct height/location of the colored bars |
| 43 |
(i.e.: adjusting the y_scale and y_shift)} |
| 44 |
|
| 45 |
\item{rowLabels}{Labels for the colorings given in \code{colors}. The labels will be printed to the |
| 46 |
left of the color rows in the plot. If the argument is given, it must be a vector of length |
| 47 |
equal to the number of columns in \code{colors}. If not given, \code{names(colors)} |
| 48 |
will be used if available. If not, sequential numbers |
| 49 |
starting from 1 will be used.} |
| 50 |
|
| 51 |
\item{cex.rowLabels}{Font size scale factor for the row labels. See \code{\link[graphics]{par}}.} |
| 52 |
|
| 53 |
\item{add}{logical(TRUE), should the colored bars be added to an existing |
| 54 |
dendrogram plot?} |
| 55 |
|
| 56 |
\item{y_scale}{how much should the bars be stretched on the y axis? |
| 57 |
If no dend is supplied - the default will be 1} |
| 58 |
|
| 59 |
\item{y_shift}{where should the bars be plotted underneath the x axis? |
| 60 |
By default it will try to locate the bars underneath the labels (it may miss, |
| 61 |
in which case you would need to enter a number manually) |
| 62 |
If no dend is supplied - the default will be 0} |
| 63 |
|
| 64 |
\item{text_shift}{a dendrogram object} |
| 65 |
|
| 66 |
\item{sort_by_labels_order}{logical(TRUE) - if TRUE (default), then the order of the |
| 67 |
colored bars will be sorted based on the order needed to change the original |
| 68 |
order of the observations to the current order of the labels in the dendrogram. |
| 69 |
If FALSE the colored bars are plotted as-is, based on the order |
| 70 |
of the colors vector.} |
| 71 |
|
| 72 |
\item{horiz}{logical (FALSE by default). Set to TRUE when using plot(dend, horiz = TRUE)} |
| 73 |
|
| 74 |
\item{...}{ignored at this point.} |
| 75 |
} |
| 76 |
\value{ |
| 77 |
An invisible vector/matrix with the ordered colors. |
| 78 |
} |
| 79 |
\description{ |
| 80 |
Add colored bars to a dendrogram, usually |
| 81 |
corresponding to either clusters or some outside |
| 82 |
categorization. |
| 83 |
} |
| 84 |
\details{ |
| 85 |
You will often needs to adjust the y_scale, y_shift and the text_shift |
| 86 |
parameters, in order to get the bars in the location you would want. |
| 87 |
|
| 88 |
(this can probably be done automatically, but will require more work. |
| 89 |
since it has to do with the current mar settings, |
| 90 |
the number of groups, and each computer's specific graphic device. |
| 91 |
patches for smarter defaults will be appreciated) |
| 92 |
} |
| 93 |
\examples{ |
| 94 |
|
| 95 |
|
| 96 |
rows_picking <- c(1:5, 25:30) |
| 97 |
dend <- (iris[rows_picking, -5] * 10) \%>\% |
| 98 |
dist() \%>\% |
| 99 |
hclust() \%>\% |
| 100 |
as.dendrogram() |
| 101 |
odd_numbers <- rows_picking \%\% 2 |
| 102 |
cols <- c("gold", "grey")[odd_numbers + 1] |
| 103 |
# scale is off |
| 104 |
plot(dend) |
| 105 |
colored_bars(cols, dend) |
| 106 |
# move and scale a bit |
| 107 |
plot(dend) |
| 108 |
colored_bars(cols, dend, |
| 109 |
y_shift = -1, |
| 110 |
rowLabels = "Odd\n numbers" |
| 111 |
) |
| 112 |
# Now let's cut the tree and add that info to the plot: |
| 113 |
k2 <- cutree(dend, k = 2) |
| 114 |
cols2 <- c("#0082CE", "#CC476B")[k2] |
| 115 |
plot(dend) |
| 116 |
colored_bars(cbind(cols2, cols), dend, |
| 117 |
rowLabels = c("2 clusters", "Odd numbers") |
| 118 |
) |
| 119 |
|
| 120 |
# The same, but with an horizontal plot! |
| 121 |
par(mar = c(6, 2, 2, 4)) |
| 122 |
plot(dend, horiz = TRUE) |
| 123 |
colored_bars(cbind(cols2, cols), dend, |
| 124 |
rowLabels = c("2 clusters", "Odd numbers"), |
| 125 |
horiz = TRUE |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
# let's add clusters color |
| 131 |
# notice how we need to play with the colors a bit |
| 132 |
# this is because color_branches places colors from |
| 133 |
# left to right. Which means we need to give colored_bars |
| 134 |
# the colors of the items so that ofter sorting they would be |
| 135 |
# from left to right. Here is how it can be done: |
| 136 |
the_k <- 3 |
| 137 |
library(colorspace) |
| 138 |
cols3 <- rainbow_hcl(the_k, c = 90, l = 50) |
| 139 |
dend \%>\% |
| 140 |
set("branches_k_color", k = the_k, with = cols3) \%>\% |
| 141 |
plot() |
| 142 |
|
| 143 |
kx <- cutree(dend, k = the_k) |
| 144 |
ord <- order.dendrogram(dend) |
| 145 |
kx <- sort_levels_values(kx[ord]) |
| 146 |
kx <- kx[match(seq_along(ord), ord)] |
| 147 |
|
| 148 |
par(mar = c(5, 5, 2, 2)) |
| 149 |
plot(dend) |
| 150 |
colored_bars(cbind(cols3[kx], cols2, cols), dend, |
| 151 |
rowLabels = c("3 clusters", "2 clusters", "Odd numbers") |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
## mtcars example |
| 157 |
|
| 158 |
# Create the dend: |
| 159 |
dend <- as.dendrogram(hclust(dist(mtcars))) |
| 160 |
|
| 161 |
# Create a vector giving a color for each car to which company it belongs to |
| 162 |
car_type <- rep("Other", length(rownames(mtcars))) |
| 163 |
is_x <- grepl("Merc", rownames(mtcars)) |
| 164 |
car_type[is_x] <- "Mercedes" |
| 165 |
is_x <- grepl("Mazda", rownames(mtcars)) |
| 166 |
car_type[is_x] <- "Mazda" |
| 167 |
is_x <- grepl("Toyota", rownames(mtcars)) |
| 168 |
car_type[is_x] <- "Toyota" |
| 169 |
car_type <- factor(car_type) |
| 170 |
n_car_types <- length(unique(car_type)) |
| 171 |
col_car_type <- colorspace::rainbow_hcl(n_car_types, c = 70, l = 50)[car_type] |
| 172 |
|
| 173 |
# extra: showing the various clusters cuts |
| 174 |
k234 <- cutree(dend, k = 2:4) |
| 175 |
|
| 176 |
# color labels by car company: |
| 177 |
labels_colors(dend) <- col_car_type[order.dendrogram(dend)] |
| 178 |
# color branches based on cutting the tree into 4 clusters: |
| 179 |
dend <- color_branches(dend, k = 4) |
| 180 |
|
| 181 |
### plots |
| 182 |
par(mar = c(12, 4, 1, 1)) |
| 183 |
plot(dend) |
| 184 |
colored_bars(cbind(k234[, 3:1], col_car_type), dend, |
| 185 |
rowLabels = c(paste0("k = ", 4:2), "Car Type") |
| 186 |
) |
| 187 |
|
| 188 |
# horiz version: |
| 189 |
par(mar = c(4, 1, 1, 12)) |
| 190 |
plot(dend, horiz = TRUE) |
| 191 |
colored_bars(cbind(k234[, 3:1], col_car_type), dend, |
| 192 |
rowLabels = c(paste0("k = ", 4:2), "Car Type"), horiz = TRUE |
| 193 |
) |
| 194 |
} |
| 195 |
\seealso{ |
| 196 |
\link{branches_attr_by_clusters}, |
| 197 |
\link[WGCNA]{plotDendroAndColors} |
| 198 |
} |
| 199 |
\author{ |
| 200 |
Steve Horvath \email{[email protected]}, |
| 201 |
Peter Langfelder \email{[email protected]}, |
| 202 |
Tal Galili \email{[email protected]} |
| 203 |
} |