Debian Codegraph @

man/colored_dots.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/colored_dots.R
3 \name{colored_dots}
4 \alias{colored_dots}
5 \title{Add colored dots beside 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 dots 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_dots(
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 dot_size = 1,
28 ...
29 )
30 }
31 \arguments{
32 \item{colors}{Coloring of the dots beside the dendrogram. Either a vector (one color per object)
33 or a matrix (can also be an array or a data frame)
34 with each column giving one group with color per object.
35 Each column will be plotted as a colored point (when horiz = FALSE)
36 under the dendrogram.
37 As long as the sort_by_labels_order paramter is TRUE (default), the colors vector/matrix should
38 be provided in the order of the original data order (and it will be re-ordered automatically to
39 the order of the dendrogram)}
40
41 \item{dend}{a dendrogram object. If missing, the colors are plotted without and re-ordering
42 (this assumes that the colors are already ordered based on the dend's labels)
43 This is also important in order to get the correct height/location of the colored dots
44 (i.e.: adjusting the y_scale and y_shift)}
45
46 \item{rowLabels}{Labels for the colorings given in \code{colors}. The labels will be printed to the
47 left of the color rows in the plot. If the argument is given, it must be a vector of length
48 equal to the number of columns in \code{colors}. If not given, \code{names(colors)}
49 will be used if available. If not, sequential numbers
50 starting from 1 will be used.}
51
52 \item{cex.rowLabels}{Font size scale factor for the row labels. See \code{\link[graphics]{par}}.}
53
54 \item{add}{logical(TRUE), should the colored dots be added to an existing
55 dendrogram plot?}
56
57 \item{y_scale}{how much should the dots be stretched on the y axis?
58 If no dend is supplied - the default will be 1}
59
60 \item{y_shift}{where should the dots be plotted underneath the x axis?
61 By default it will try to locate the dots underneath the labels (it may miss,
62 in which case you would need to enter a number manually)
63 If no dend is supplied - the default will be 0}
64
65 \item{text_shift}{a dendrogram object}
66
67 \item{sort_by_labels_order}{logical(TRUE) - if TRUE (default), then the order of the
68 colored dots will be sorted based on the order needed to change the original
69 order of the observations to the current order of the labels in the dendrogram.
70 If FALSE the colored dots are plotted as-is, based on the order
71 of the colors vector.}
72
73 \item{horiz}{logical (FALSE by default). Set to TRUE when using plot(dend, horiz = TRUE)}
74
75 \item{dot_size}{numeric (1 by default). Passed to cex argument in points}
76
77 \item{...}{ignored at this point.}
78 }
79 \value{
80 An invisible vector/matrix with the ordered colors.
81 }
82 \description{
83 Add colored dots next to a dendrogram, usually
84 corresponding to either clusters or some outside
85 categorization.
86 }
87 \details{
88 The reason you might choose colored_dots over colored_bars is when you have
89 a lot of group types and/or a really large dendrogram.
90 Hint: Make a group for each categorical factor and color it one color when true,
91 and assign a fully transparent color when false.
92
93 You will often need to adjust the y_scale, y_shift and the text_shift
94 parameters, in order to get the dots in the location you would want.
95
96 (This can probably be done automatically, but will require more work.
97 since it has to do with the current mar settings,
98 the number of groups, and each computer's specific graphic device.
99 patches for smarter defaults will be appreciated)
100 }
101 \examples{
102
103 rows_picking <- c(1:5, 25:30)
104 dend <- (iris[rows_picking, -5] * 10) \%>\%
105 dist() \%>\%
106 hclust() \%>\%
107 as.dendrogram()
108 odd_numbers <- rows_picking \%\% 2
109 cols <- c("red", "white")[odd_numbers + 1]
110 plot(dend)
111 colored_dots(cols, dend)
112 # Example of adjusting postion of dots
113 plot(dend)
114 colored_dots(cols, dend,
115 y_shift = -1,
116 rowLabels = "Odd\n numbers"
117 )
118
119
120
121 rows_picking <- c(1:5, 25:30)
122 dend <- (iris[rows_picking, -5] * 10) \%>\%
123 dist() \%>\%
124 hclust() \%>\%
125 as.dendrogram()
126 odd_numbers <- rows_picking \%\% 2
127 # For leaves that shouldn't have dots, make them the same color as the background,
128 # or set the alpha value to fully transparant
129 cols <- c("black", "white")[odd_numbers + 1]
130 # scale is off
131 plot(dend)
132 colored_dots(cols, dend)
133 # move and scale a bit
134 plot(dend)
135 colored_dots(cols, dend,
136 y_shift = -1,
137 rowLabels = "Odd\n numbers"
138 )
139 # Now let's cut the tree and add that info to the plot:
140 k2 <- cutree(dend, k = 2)
141 cols2 <- c("#1b9e77", "#d95f02")[k2]
142
143 par(mar = c(5, 6, 1, 1))
144 plot(dend)
145 colored_dots(cbind(cols2, cols), dend,
146 rowLabels = c("2 clusters", "Even numbers")
147 )
148
149 # The same, but with an horizontal plot!
150 par(mar = c(6, 2, 2, 4))
151 plot(dend, horiz = TRUE)
152 colored_dots(cbind(cols2, cols), dend,
153 rowLabels = c("2 clusters", "Even numbers"),
154 horiz = TRUE
155 )
156
157 # ==============================
158 # ==============================
159
160 ## mtcars example
161
162 # Create the dend:
163 dend <- as.dendrogram(hclust(dist(mtcars)))
164
165 # Get all company names
166 comp_names <- unlist(lapply(rownames(mtcars), function(x) strsplit(x, " ")[[1]][[1]]))
167 # Get the top three occurring companies
168 top_three <- sort(table(comp_names), decreasing = TRUE)[1:3]
169 # Match the top three companies to where they are found in the dendrogram labels
170 top_three <- sapply(names(top_three), function(x) grepl(x, labels(dend)))
171 top_three <- as.data.frame(top_three)
172 # "top_three" is now a data frame of the top three companies as columns.
173 # Each column represents a vector (rows) which is the length of labels(dend).
174 # The vector has values TRUE and FALSE, for whether the company name matched
175 # labels(dend)[i]
176
177 # Colorblind friendly vector of HEX colors
178 colorblind_friendly <- c("#1b9e77", "#d95f02", "#7570b3")
179
180 # If we run the for-loop on "top_three" we will turn the vectors into a character-type too early,
181 # so make a copy to "colored_dataframe" which we will work on
182 colored_dataframe <- top_three
183
184 for (i in 1:3) {
185 # This replaces TRUE values with a color from our vector of colors
186 colored_dataframe[top_three[, i], i] <- colorblind_friendly[[i]]
187 # This replaces FALSE values with black HEX, but fully transparent (invisible on plot)
188 colored_dataframe[!top_three[, i], i] <- "#00000000"
189 }
190
191 # Color branches and labels by "cutting" the dendrogram at an arbitrary height
192 dend <- color_branches(dend, h = 170)
193 dend <- color_labels(dend, h = 170)
194
195 ### plots
196 par(mar = c(12, 4, 1, 1))
197 plot(dend)
198 colored_dots(colored_dataframe, dend,
199 rowLabels = colnames(colored_dataframe), horiz = FALSE, sort_by_labels_order = FALSE
200 )
201 # Show a dotted line where tree was "cut"
202 abline(h = 170, lty = 3)
203
204 # horiz version:
205 par(mar = c(4, 1, 1, 12))
206 plot(dend, horiz = TRUE)
207 colored_dots(colored_dataframe, dend,
208 rowLabels = colnames(colored_dataframe), horiz = TRUE, sort_by_labels_order = FALSE
209 )
210 # Show a dotted line where the tree was "cut"
211 abline(v = 170, lty = 3)
212 }
213 \seealso{
214 \link{branches_attr_by_clusters},
215 \link[WGCNA]{plotDendroAndColors}
216 }
217 \author{
218 Steve Horvath \email{[email protected]},
219 Tal Galili \email{[email protected]},
220 Peter Langfelder \email{[email protected]},
221 Chase Clark \email{[email protected]}
222 }