1
我想在R中使用相關矩陣和p值矩陣構造熱圖。熱圖中的邊框單元
我使用這個tutorial來構建沒有問題的熱圖。它完美的作品。我甚至可以在右邊的單元格中引入來自第二個矩陣(p值矩陣)的一些值。
但是,當我嘗試突出顯示相應的單元格時,它不起作用。我使用這個code來生成邊框。 我將RStudio v0.97與包gplots,RColorBrewer一起使用。 的代碼是:
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
我覺得有什麼不對或者與makeRects
功能,或通過heatmap.2
功能行的重新排序。矩形出現在熱圖中,但它們不在正確的位置。我一整天都在撓頭,卻沒有發現什麼是錯的。
有什麼建議嗎?
感謝您的回答。我編輯了我的帖子,以增加對所使用的變量和包的清晰度。由於我不理解的原因,heatmap.2似乎改變了熱圖的方向,這會混淆makeRects函數。當我使用熱圖功能時,它可以工作(但我不能擁有我想要的選項)... – user2617763
「Rowv = FALSE」後我錯過了一個逗號(我爲此提交了一個編輯)。如果你根本不想要樹狀圖,你應該設置'dendrogram =「none」'。如果'heatmap.2()'改變方向,轉置你給它的矩陣,'mat1t = t(mat1)'和'mat2t = t(mat2)',然後'mat1t'和'mat2t'提供給'在代碼中使用heatmap.2()'和'makeRects()'函數。 –
我不想轉置我的主矩陣,因爲我喜歡它的方向。我嘗試轉置次矩陣,但它也沒有工作。最後我通過顛倒次矩陣來得到它:'rmat2 < - apply(mat2,2,rev)',然後轉置它:'trmat2 < - t(rmat2)'。它終於得到了一切正確的順序,雖然我不知道如何以及爲什麼...... – user2617763