I want to visualize a 42×42 matrix as 28 separate heatmaps, each heatmap being 6×6 matrix with the values plotted on the top of colours. I only need lower half of he matrix, I don’t want to plot anything that has been excluded. The subsequent 6×6 matrixes shouldn’t overlap, as in the example below:
d = as.matrix(read.table("http://dl.dropbox.com/u/2505196/matrix_posthoc_tukey.dat"))
d[upper.tri(d)] <- NA
d1 <- d[1:6, 1:6]
d2 <- d[1:6, 7:12]
d3 <- d[1:6, 13:18]
d4 <- d[1:6, 18:24]
#...etc, up to d28 <- d[37:42,37:42]
Code I used to create a single heatmap looks like this:
#baseline to create a separated space for all 28 plots
par(mfrow=c(4,7), mar=c(2,2,4,1), oma=c(2,4,2,2))
#using `image` to create heatmap, with color breaks defined by specific values
#the code below create just single heatmap
image(x=1:6, y=1:6, axes = FALSE, ylab="", xlab="", d1,
breaks=c(min(d1,na.rm=TRUE), -5.45, -4.65, 4.65, 5.45, max(d1,na.rm=TRUE)),
col=c("red","orange","white","orange","red"))
axis(2, 1:6, cex.axis = 0.7, las=1, tick=F)
axis(3, 1:6, cex.axis = 0.7, tick=F)
#create vertical and forizontal lines
abline(h=seq(0.5,6.5,1), v=seq(0.5,6.5,1))
#plot values from the specific matrix subset
for (i in 1:6)
{
for (j in 1:6)
{
txt <- sprintf("%0.1f", d1[i,j])
text(i, j, txt, cex=0.7)
}
}
Three such heatmaps look like this:

That’s where I’m stuck. I have to manually change d value every time I add another image to my single-page, multiple heatmap collection. I don’t know how to create a nice loop to plot those specific subsets of matrix at the same time using the code above.
Alternative solutions with ggplot2, lattice are also welcomed, although I believe the main question here is a good loop to make this series of heatmaps.
This is quite a complex plot, but it can be readily produced by the standard graphics library in R. It is more or less only a matter of keeping track of what indices goes into which panel. The way you extract the
d1tod28matrices can be automated so you don’t have to write out each and every line.The numbers in each cell may be tiny, but if you plot it to a pdf and zoom in they can be read. The
xpdparameter of thesegmentsfunctions supressess R from clipping the lines to the plot area (otherwise the outer lines would appear slightly thinner).