|
a |
|
b/man/pheatmapwh.Rd |
|
|
1 |
\name{pheatmapwh} |
|
|
2 |
\alias{pheatmapwh} |
|
|
3 |
\title{A modification of \code{\link[pheatmap]{pheatmap}} from the pheatmap package by Raivo Kolde: draw clustered heatmaps.} |
|
|
4 |
\usage{ |
|
|
5 |
pheatmapwh(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = |
|
|
6 |
"RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60", |
|
|
7 |
cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE, |
|
|
8 |
cluster_cols = TRUE, clustering_distance_rows = "euclidean", |
|
|
9 |
clustering_distance_cols = "euclidean", clustering_method = "complete", |
|
|
10 |
clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA, |
|
|
11 |
treeheight_row = ifelse(cluster_rows, 50, 0), |
|
|
12 |
treeheight_col = ifelse(cluster_cols, 50, 0), legend = TRUE, |
|
|
13 |
legend_breaks = NA, legend_labels = NA, annotation_row = NA, |
|
|
14 |
annotation_col = NA, annotation = NA, annotation_colors = NA, |
|
|
15 |
annotation_legend = TRUE, drop_levels = TRUE, show_rownames = T, |
|
|
16 |
show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize, |
|
|
17 |
fontsize_col = fontsize, display_numbers = F, number_format = "\%.2f", |
|
|
18 |
number_color = "grey30", fontsize_number = 0.8 * fontsize, |
|
|
19 |
gaps_row = NULL, gaps_col = NULL, labels_row = NULL, |
|
|
20 |
labels_col = NULL, filename = NA, width = NA, height = NA, |
|
|
21 |
silent = FALSE, ...) |
|
|
22 |
} |
|
|
23 |
\arguments{ |
|
|
24 |
\item{mat}{numeric matrix of the values to be plotted.} |
|
|
25 |
|
|
|
26 |
\item{color}{vector of colors used in heatmap.} |
|
|
27 |
|
|
|
28 |
\item{kmeans_k}{the number of kmeans clusters to make, if we want to agggregate the |
|
|
29 |
rows before drawing heatmap. If NA then the rows are not aggregated.} |
|
|
30 |
|
|
|
31 |
\item{breaks}{a sequence of numbers that covers the range of values in mat and is one |
|
|
32 |
element longer than color vector. Used for mapping values to colors. Useful, if needed |
|
|
33 |
to map certain values to certain colors, to certain values. If value is NA then the |
|
|
34 |
breaks are calculated automatically.} |
|
|
35 |
|
|
|
36 |
\item{border_color}{color of cell borders on heatmap, use NA if no border should be |
|
|
37 |
drawn.} |
|
|
38 |
|
|
|
39 |
\item{cellwidth}{individual cell width in points. If left as NA, then the values |
|
|
40 |
depend on the size of plotting window.} |
|
|
41 |
|
|
|
42 |
\item{cellheight}{individual cell height in points. If left as NA, |
|
|
43 |
then the values depend on the size of plotting window.} |
|
|
44 |
|
|
|
45 |
\item{scale}{character indicating if the values should be centered and scaled in |
|
|
46 |
either the row direction or the column direction, or none. Corresponding values are |
|
|
47 |
\code{"row"}, \code{"column"} and \code{"none"}} |
|
|
48 |
|
|
|
49 |
\item{cluster_rows}{boolean values determining if rows should be clustered,} |
|
|
50 |
|
|
|
51 |
\item{cluster_cols}{boolean values determining if columns should be clustered.} |
|
|
52 |
|
|
|
53 |
\item{clustering_distance_rows}{distance measure used in clustering rows. Possible |
|
|
54 |
values are \code{"correlation"} for Pearson correlation and all the distances |
|
|
55 |
supported by \code{\link{dist}}, such as \code{"euclidean"}, etc. If the value is none |
|
|
56 |
of the above it is assumed that a distance matrix is provided.} |
|
|
57 |
|
|
|
58 |
\item{clustering_distance_cols}{distance measure used in clustering columns. Possible |
|
|
59 |
values the same as for clustering_distance_rows.} |
|
|
60 |
|
|
|
61 |
\item{clustering_method}{clustering method used. Accepts the same values as |
|
|
62 |
\code{\link{hclust}}.} |
|
|
63 |
|
|
|
64 |
\item{clustering_callback}{callback function to modify the clustering. Is |
|
|
65 |
called with two parameters: original \code{hclust} object and the matrix |
|
|
66 |
used for clustering. Must return a \code{hclust} object.} |
|
|
67 |
|
|
|
68 |
\item{cutree_rows}{number of clusters the rows are divided into, based on the |
|
|
69 |
hierarchical clustering (using cutree), if rows are not clustered, the |
|
|
70 |
argument is ignored} |
|
|
71 |
|
|
|
72 |
\item{cutree_cols}{similar to \code{cutree_rows}, but for columns} |
|
|
73 |
|
|
|
74 |
\item{treeheight_row}{the height of a tree for rows, if these are clustered. |
|
|
75 |
Default value 50 points.} |
|
|
76 |
|
|
|
77 |
\item{treeheight_col}{the height of a tree for columns, if these are clustered. |
|
|
78 |
Default value 50 points.} |
|
|
79 |
|
|
|
80 |
\item{legend}{logical to determine if legend should be drawn or not.} |
|
|
81 |
|
|
|
82 |
\item{legend_breaks}{vector of breakpoints for the legend.} |
|
|
83 |
|
|
|
84 |
\item{legend_labels}{vector of labels for the \code{legend_breaks}.} |
|
|
85 |
|
|
|
86 |
\item{annotation_row}{data frame that specifies the annotations shown on left |
|
|
87 |
side of the heatmap. Each row defines the features for a specific row. The |
|
|
88 |
rows in the data and in the annotation are matched using corresponding row |
|
|
89 |
names. Note that color schemes takes into account if variable is continuous |
|
|
90 |
or discrete.} |
|
|
91 |
|
|
|
92 |
\item{annotation_col}{similar to annotation_row, but for columns.} |
|
|
93 |
|
|
|
94 |
\item{annotation}{deprecated parameter that currently sets the annotation_col if it is missing} |
|
|
95 |
|
|
|
96 |
\item{annotation_colors}{list for specifying annotation_row and |
|
|
97 |
annotation_col track colors manually. It is possible to define the colors |
|
|
98 |
for only some of the features. Check examples for details.} |
|
|
99 |
|
|
|
100 |
\item{annotation_legend}{boolean value showing if the legend for annotation |
|
|
101 |
tracks should be drawn.} |
|
|
102 |
|
|
|
103 |
\item{drop_levels}{logical to determine if unused levels are also shown in |
|
|
104 |
the legend} |
|
|
105 |
|
|
|
106 |
\item{show_rownames}{boolean specifying if column names are be shown.} |
|
|
107 |
|
|
|
108 |
\item{show_colnames}{boolean specifying if column names are be shown.} |
|
|
109 |
|
|
|
110 |
\item{main}{the title of the plot} |
|
|
111 |
|
|
|
112 |
\item{fontsize}{base fontsize for the plot} |
|
|
113 |
|
|
|
114 |
\item{fontsize_row}{fontsize for rownames (Default: fontsize)} |
|
|
115 |
|
|
|
116 |
\item{fontsize_col}{fontsize for colnames (Default: fontsize)} |
|
|
117 |
|
|
|
118 |
\item{display_numbers}{logical determining if the numeric values are also printed to |
|
|
119 |
the cells. If this is a matrix (with same dimensions as original matrix), the contents |
|
|
120 |
of the matrix are shown instead of original values.} |
|
|
121 |
|
|
|
122 |
\item{number_format}{format strings (C printf style) of the numbers shown in cells. |
|
|
123 |
For example "\code{\%.2f}" shows 2 decimal places and "\code{\%.1e}" shows exponential |
|
|
124 |
notation (see more in \code{\link{sprintf}}).} |
|
|
125 |
|
|
|
126 |
\item{number_color}{color of the text} |
|
|
127 |
|
|
|
128 |
\item{fontsize_number}{fontsize of the numbers displayed in cells} |
|
|
129 |
|
|
|
130 |
\item{gaps_row}{vector of row indices that show shere to put gaps into |
|
|
131 |
heatmap. Used only if the rows are not clustered. See \code{cutree_row} |
|
|
132 |
to see how to introduce gaps to clustered rows.} |
|
|
133 |
|
|
|
134 |
\item{gaps_col}{similar to gaps_row, but for columns.} |
|
|
135 |
|
|
|
136 |
\item{labels_row}{custom labels for rows that are used instead of rownames.} |
|
|
137 |
|
|
|
138 |
\item{labels_col}{similar to labels_row, but for columns.} |
|
|
139 |
|
|
|
140 |
\item{filename}{file path where to save the picture. Filetype is decided by |
|
|
141 |
the extension in the path. Currently following formats are supported: png, pdf, tiff, |
|
|
142 |
bmp, jpeg. Even if the plot does not fit into the plotting window, the file size is |
|
|
143 |
calculated so that the plot would fit there, unless specified otherwise.} |
|
|
144 |
|
|
|
145 |
\item{width}{manual option for determining the output file width in inches.} |
|
|
146 |
|
|
|
147 |
\item{height}{manual option for determining the output file height in inches.} |
|
|
148 |
|
|
|
149 |
\item{silent}{do not draw the plot (useful when using the gtable output)} |
|
|
150 |
|
|
|
151 |
\item{\dots}{graphical parameters for the text used in plot. Parameters passed to |
|
|
152 |
\code{\link{grid.text}}, see \code{\link{gpar}}.} |
|
|
153 |
} |
|
|
154 |
\value{ |
|
|
155 |
Invisibly a list of components |
|
|
156 |
\itemize{ |
|
|
157 |
\item \code{tree_row} the clustering of rows as \code{\link{hclust}} object |
|
|
158 |
\item \code{tree_col} the clustering of columns as \code{\link{hclust}} object |
|
|
159 |
\item \code{kmeans} the kmeans clustering of rows if parameter \code{kmeans_k} was |
|
|
160 |
specified |
|
|
161 |
} |
|
|
162 |
} |
|
|
163 |
\description{ |
|
|
164 |
A function to draw clustered heatmaps where one has better control over some graphical |
|
|
165 |
parameters such as cell size, etc. |
|
|
166 |
} |
|
|
167 |
\details{ |
|
|
168 |
The function also allows to aggregate the rows using kmeans clustering. This is |
|
|
169 |
advisable if number of rows is so big that R cannot handle their hierarchical |
|
|
170 |
clustering anymore, roughly more than 1000. Instead of showing all the rows |
|
|
171 |
separately one can cluster the rows in advance and show only the cluster centers. |
|
|
172 |
The number of clusters can be tuned with parameter kmeans_k. |
|
|
173 |
} |
|
|
174 |
\examples{ |
|
|
175 |
# Create test matrix |
|
|
176 |
test = matrix(rnorm(200), 20, 10) |
|
|
177 |
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 |
|
|
178 |
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2 |
|
|
179 |
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4 |
|
|
180 |
colnames(test) = paste("Test", 1:10, sep = "") |
|
|
181 |
rownames(test) = paste("Gene", 1:20, sep = "") |
|
|
182 |
|
|
|
183 |
# Draw heatmaps |
|
|
184 |
pheatmapwh(test) |
|
|
185 |
pheatmapwh(test, kmeans_k = 2) |
|
|
186 |
pheatmapwh(test, scale = "row", clustering_distance_rows = "correlation") |
|
|
187 |
pheatmapwh(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50)) |
|
|
188 |
pheatmapwh(test, cluster_row = FALSE) |
|
|
189 |
pheatmapwh(test, legend = FALSE) |
|
|
190 |
|
|
|
191 |
# Show text within cells |
|
|
192 |
pheatmapwh(test, display_numbers = TRUE) |
|
|
193 |
pheatmapwh(test, display_numbers = TRUE, number_format = "\\\%.1e") |
|
|
194 |
pheatmapwh(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))) |
|
|
195 |
pheatmapwh(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0", |
|
|
196 |
"1e-4", "1e-3", "1e-2", "1e-1", "1")) |
|
|
197 |
|
|
|
198 |
# Fix cell sizes and save to file with correct size |
|
|
199 |
pheatmapwh(test, cellwidth = 15, cellheight = 12, main = "Example heatmap") |
|
|
200 |
pheatmapwh(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") |
|
|
201 |
|
|
|
202 |
# Generate annotations for rows and columns |
|
|
203 |
annotation_col = data.frame( |
|
|
204 |
CellType = factor(rep(c("CT1", "CT2"), 5)), |
|
|
205 |
Time = 1:5 |
|
|
206 |
) |
|
|
207 |
rownames(annotation_col) = paste("Test", 1:10, sep = "") |
|
|
208 |
|
|
|
209 |
annotation_row = data.frame( |
|
|
210 |
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))) |
|
|
211 |
) |
|
|
212 |
rownames(annotation_row) = paste("Gene", 1:20, sep = "") |
|
|
213 |
|
|
|
214 |
# Display row and color annotations |
|
|
215 |
pheatmapwh(test, annotation_col = annotation_col) |
|
|
216 |
pheatmapwh(test, annotation_col = annotation_col, annotation_legend = FALSE) |
|
|
217 |
pheatmapwh(test, annotation_col = annotation_col, annotation_row = annotation_row) |
|
|
218 |
|
|
|
219 |
|
|
|
220 |
# Specify colors |
|
|
221 |
ann_colors = list( |
|
|
222 |
Time = c("white", "firebrick"), |
|
|
223 |
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"), |
|
|
224 |
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E") |
|
|
225 |
) |
|
|
226 |
|
|
|
227 |
pheatmapwh(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title") |
|
|
228 |
pheatmapwh(test, annotation_col = annotation_col, annotation_row = annotation_row, |
|
|
229 |
annotation_colors = ann_colors) |
|
|
230 |
pheatmapwh(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) |
|
|
231 |
|
|
|
232 |
# Gaps in heatmaps |
|
|
233 |
pheatmapwh(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14)) |
|
|
234 |
pheatmapwh(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), |
|
|
235 |
cutree_col = 2) |
|
|
236 |
|
|
|
237 |
# Show custom strings as row/col names |
|
|
238 |
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", |
|
|
239 |
"", "", "Il10", "Il15", "Il1b") |
|
|
240 |
|
|
|
241 |
pheatmapwh(test, annotation_col = annotation_col, labels_row = labels_row) |
|
|
242 |
|
|
|
243 |
# Specifying clustering from distance matrix |
|
|
244 |
drows = dist(test, method = "minkowski") |
|
|
245 |
dcols = dist(t(test), method = "minkowski") |
|
|
246 |
pheatmapwh(test, clustering_distance_rows = drows, clustering_distance_cols = dcols) |
|
|
247 |
|
|
|
248 |
# Modify ordering of the clusters using clustering callback option |
|
|
249 |
callback = function(hc, mat){ |
|
|
250 |
sv = svd(t(mat))$v[,1] |
|
|
251 |
dend = reorder(as.dendrogram(hc), wts = sv) |
|
|
252 |
as.hclust(dend) |
|
|
253 |
} |
|
|
254 |
|
|
|
255 |
pheatmapwh(test, clustering_callback = callback) |
|
|
256 |
|
|
|
257 |
\dontrun{ |
|
|
258 |
# Same using dendsort package |
|
|
259 |
library(dendsort) |
|
|
260 |
|
|
|
261 |
callback = function(hc, ...){dendsort(hc)} |
|
|
262 |
pheatmapwh(test, clustering_callback = callback) |
|
|
263 |
} |
|
|
264 |
} |
|
|
265 |
\author{ |
|
|
266 |
Raivo Kolde <rkolde@gmail.com> |
|
|
267 |
} |
|
|
268 |
|