Upper triangular matrix heatmap using pheatmap package
0
0
Entering edit mode
@deepak-tanwar-8113
Last seen 17 months ago
McGill University, Canada

How can I plot only upper triangular portion of a gene by gene correlation matrix with pheatmap package ?

Even if I try to remove lower triangle by: mat[lower.tri(mat)] <- "NA"  it does not work.

pheatmap • 7.7k views
ADD COMMENT
1
Entering edit mode

It probably will work if you remove quotation marks around NA. Also turn off the clustering of rows and columns to see nice triangular plot.

ADD REPLY
0
Entering edit mode

It does work, but I want the rows and columns to be clustered. 

ADD REPLY
1
Entering edit mode

Then you have to cluster the data before you assign some values NA. For example assuming that mat is full correlation matrix you can do the following

hc = hclust(as.dist(1 - mat))
mat = mat[hc$order, hc$order]
mat[lower.tri(mat)] = NA

pheatmap(mat, cluster_col = F, cluster_row = F)
ADD REPLY
0
Entering edit mode

It's perfect Raivo. 

Is there a way to also show dendogram along with heatmap?

I tried 

pheatmap(mat, cluster_col = hc$order, cluster_row = hc$order)


ADD REPLY
1
Entering edit mode

It is a hack but basically you can achieve it like this (starting from full correaltion matrix again) 

o = rownames(mat)
hc = hclust(as.dist(1 - mat))
mat = mat[hc$order, hc$order]
mat[lower.tri(mat)] = NA
mat = mat[o, o]

pheatmap(mat, cluster_col = hc, cluster_row = hc)
ADD REPLY
0
Entering edit mode

I tried to perform the code, but I am getting following error. I also tried to look for the error:

Error in if (cluster_rows) { : argument is not interpretable as logical
In addition: Warning message:
In if (cluster_rows) { :
  the condition has length > 1 and only the first element will be used
ADD REPLY
0
Entering edit mode

Hi,

I am not yet able to figure out that:

test = matrix(rnorm(200), 20, 10)

test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3

test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2

test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4

colnames(test) = paste("Test", 1:10, sep = "")

rownames(test) = paste("Gene", 1:20, sep = "")



mat <- cor(t(test))
o = rownames(mat)
hc = hclust(as.dist(1 - mat))
mat = mat[hc$order, hc$order]
mat[lower.tri(mat)] = NA
mat = mat[o, o]

pheatmap(mat, cluster_rows = hc, cluster_cols = hc)

 

Error in if (cluster_rows) { : argument is not interpretable as logical
In addition: Warning message:
In if (cluster_rows) { :
  the condition has length > 1 and only the first element will be used

 

ADD REPLY
1
Entering edit mode

You have to update your version of pheatmap. This feature of giving clustering as a parameter was introduces fairly recently. 

ADD REPLY
0
Entering edit mode

So, I will update it from github.

ADD REPLY

Login before adding your answer.

Traffic: 531 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6