Search code examples
rggplot2heatmapgplots

ignore values below threshold in adjacency matrix for heatmaps in R


I have an adjacency matrix (netm) with co-occurrences as mostly 0's. I get the heatmap below when I plot it using:

require(gplots)
heatmap.2(netm,col=c("gold", "dark orange","orange","yellow"),
    Rowv=F, Colv=F, dendrogram="none", scale="none", trace="none")

How can I ignore values below a certain threshold in the matrix? I don't want to plot values below 3 in my graph co-occurrence matrix.

enter image description here

Snapshot of data (a co occurence matrix )

    bacardi breezer aldi    rum white   coconut
bacardi 0   2   0   1   0   0
breezer 2   0   0   0   0   0
aldi    0   0   0   1   1   0
rum 1   0   1   0   1   1
white   0   0   1   1   0   0
coconut 0   0   0   1   0   0
drinks  0   0   0   1   0   1
daniel  0   0   0   1   0   0

Solution

  • Either you can substitute NA to unwanted values (e.g. 0s), and keep them in the plot:

    netm2 <- netm
    netm2[netm2 == 0] <- NA
    heatmap.2(netm2, col=c("gold", "dark orange","orange","yellow"), Rowv=F, Colv=F, dendrogram="none", scale="none", trace="none")
    

    or manually remove columns/rows which contain NAs:

    netm3 <- netm2[complete.cases(netm2), complete.cases(t(netm2))]
    heatmap.2(netm3, col=c("gold", "dark orange","orange","yellow"), Rowv=F, Colv=F, dendrogram="none", scale="none", trace="none")