I am trying to construct a heatmap in R using a correlation matrix and ap 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?
It would be helpful if you included some example data for folks to run your code on, and you should also specify any packages that are required to run your code. For example,
library(gplots)
library(Hmisc)
x <- matrix(rnorm(100), ncol = 5)
y <- matrix(rnorm(100), ncol = 5)
keep <- rcorr(x, y)
matrr <- keep$r
matrp <- keep$P
In your makeRects()
function you refer to nx
and ny
, but they are never defined.
In your call to heatmap.2()
you refer to matrl
, but it is never defined.
If you add definitions for these three objects to your question, we may be able to troubleshoot your plotting issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.