I am trying to construct a heatmap in R using a correlation matrix and a p value matrix.
I use this tutorial to build the heatmap without problems. It works perfectly. I can even introduce some values from a second matrix (the p value matrix) in the right cells.
But when I try to highlight the corresponding cells it doesn't work. I use this code to generate the borders.
I use RStudio v0.97 with the packages gplots, RColorBrewer.
The code is :
library(gplots)
library(RColorBrewer)
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
nx = 5
ny = 10
mat1 <- matrix(rnorm(nx*ny,100,50),nx,ny)
mat2 <- mat1>150 #matrix used for the example instead of the p value matrix
makeRects <- function(tfMat,border){
cAbove = expand.grid(1:nx,1:ny)[tfMat,]
xl=cAbove[,1]-0.49
yb=cAbove[,2]-0.49
xr=cAbove[,1]+0.49
yt=cAbove[,2]+0.49
rect(xl,yb,xr,yt,border=border,lwd=3)
} #this is the function to make the rectangles/borders
heatmap.2(mat1,
Rowv = FALSE, # don't reorganize columns
cellnote = mat2, # check correspondance between rectangles and mat2 values
main = "Correlation", # heat map title
notecol="black", # change font color of cell labels to black
notecex=0.5, # change the scaling of the cell labels
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(12,9), # widens margins around plot
col=my_palette, # use on color palette defined earlier
dendrogram="row", # don't draw a row dendrogram
Colv="NA", # turn off column clustering
add.expr = {makeRects(mat2,"black")}) #add the borders
I think something is wrong either with the makeRects
function, or with the re-ordering of the rows via the heatmap.2
function. The rectangles appear in the heatmap, but they are not at the right positions. I have been scratching my head all day without finding what is wrong.
Any suggestions?