Search
Question: Upper triangular matrix heatmap using pheatmap package
0
gravatar for Deepak Tanwar
17 months ago by
University of Alabama at Birminghan, USA
Deepak Tanwar10 wrote:

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.

ADD COMMENTlink modified 17 months ago • written 17 months ago by Deepak Tanwar10
1

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 REPLYlink written 17 months ago by Raivo Kolde50

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

ADD REPLYlink written 17 months ago by Deepak Tanwar10
1

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 REPLYlink written 17 months ago by Raivo Kolde50

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 REPLYlink written 17 months ago by Deepak Tanwar10
1

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 REPLYlink written 17 months ago by Raivo Kolde50

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 REPLYlink written 17 months ago by Deepak Tanwar10

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 REPLYlink written 17 months ago by Deepak Tanwar10
1

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

ADD REPLYlink written 17 months ago by Raivo Kolde50

So, I will update it from github.

ADD REPLYlink written 17 months ago by Deepak Tanwar10
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 163 users visited in the last hour