Search
Question: Upper triangular matrix heatmap using pheatmap package
0
3.0 years ago by
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.

modified 3.0 years ago • written 3.0 years 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.

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

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)

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)


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)

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 if (cluster_rows) { :
the condition has length > 1 and only the first element will be used

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

1

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