# This example illustrates 4 main ways of annotating columns and rows of a heatmap.
# Copy and paste the whole example into an R session with an interactive plot window;
# alternatively, you may replace the command sizeGrWindow below by opening
# another graphical device such as pdf.
# Generate a matrix to be plotted
nCol = 8; nRow = 7;
mat = matrix(runif(nCol*nRow, min = -1, max = 1), nRow, nCol);
rowColors = standardColors(nRow);
colColors = standardColors(nRow + nCol)[(nRow+1):(nRow + nCol)];
rowColors;
colColors;
sizeGrWindow(9,7)
par(mfrow = c(2,2))
par(mar = c(4, 5, 4, 6));
# Label rows and columns by text:
labeledHeatmap(mat, xLabels = colColors, yLabels = rowColors,
colors = greenWhiteRed(50),
setStdMargins = FALSE,
textMatrix = signif(mat, 2),
main = "Text-labeled heatmap");
# Label rows and columns by colors:
rowLabels = paste("ME", rowColors, sep="");
colLabels = paste("ME", colColors, sep="");
labeledHeatmap(mat, xLabels = colLabels, yLabels = rowLabels,
colorLabels = TRUE,
colors = greenWhiteRed(50),
setStdMargins = FALSE,
textMatrix = signif(mat, 2),
main = "Color-labeled heatmap");
# Mix text and color labels:
rowLabels[3] = "Row 3";
colLabels[1] = "Column 1";
labeledHeatmap(mat, xLabels = colLabels, yLabels = rowLabels,
colorLabels = TRUE,
colors = greenWhiteRed(50),
setStdMargins = FALSE,
textMatrix = signif(mat, 2),
main = "Mix-labeled heatmap");
# Color labels and additional text labels
rowLabels = paste("ME", rowColors, sep="");
colLabels = paste("ME", colColors, sep="");
extraRowLabels = paste("Row", c(1:nRow));
extraColLabels = paste("Column", c(1:nCol));
# Extend margins to fit all labels
par(mar = c(6, 6, 4, 6));
labeledHeatmap(mat, xLabels = colLabels, yLabels = rowLabels,
xSymbols = extraColLabels,
ySymbols = extraRowLabels,
colorLabels = TRUE,
colors = greenWhiteRed(50),
setStdMargins = FALSE,
textMatrix = signif(mat, 2),
main = "Text- + color-labeled heatmap");
Run the code above in your browser using DataLab