Search code examples
rheatmapbioconductor

Binary Data heatmap


Can anyone tell me how to plot a heatmap for binary data, similar to heatmap in this link- Binary R heatmap still displays gradient ,

I tried to do, but I suppose I am not able to give the file input properly. Here is the link to the data I want to plot- https://www.dropbox.com/s/7k1uskwrfuaugw3/Dataset.csv?dl=0

Here is a subset of my data-

Strains gene1   gene2   gene3   gene4   gene5
strain1 1   1   1   1   1
strain2 1   1   1   1   1
strain3 1   1   1   1   1
strain4 1   1   1   1   1
strain5 1   1   1   1   1
strain6 1   1   1   1   1
strain7 1   1   1   1   1
strain8 1   1   1   1   1
strain9 1   1   0   0   0

And the output I am getting:

enter image description here

library(gplots)
file1<- read.csv('Dataset.csv',header = T)
class(file1)
dat <- data.frame(file1)
dim(dat)
names(dat)
head(dat)
rownames(dat) <-dat$Strains
head(dat)
dim(dat)
head(dat)
dat.tdy <- dat[,2:26]
dat.n <- scale(t(dat.tdy))
dat.tn <- t(dat.n)
col = c("black", "grey")
row_names <- rownames(dat.tn)
heatmap.2(dat.tn, scale = "none", Rowv = NA, Colv = NA, col = c("black", "grey"), margin=c(6, 4),trace='none',labRow = row_names,
      lhei=c(1,4),cexRow = 1,cexCol = 1,
      lwid=c(.1,1), keysize=0.1, key.par = list(cex=0.5), sepwidth=c(0.1,0.1),
      sepcolor="white",
      colsep=1:ncol(dat),
      rowsep=1:nrow(dat))

This code is running properly and giving an output, but when I cross check with the input file, I see that the color matrix in heatmap is different than the input file. For example in the heatmap, for gene7 there is only one black box, but it actually has almost 13 zeros in the input file.

I feel there is a simpler way to do it..but as I am new to R, I am not able to figure it out. I am doing something wrong in giving the input file. Please help. Thanks


Solution

  • I found a much simple code to plot the binary heatmap-

    library(d3heatmap)
    x<- read.csv("Dataset.csv", header = T, row.names = 1)
    d3heatmap(x, Colv = NA,Rowv = NA, col = c("blue", "red"), scale="none", cexRow = 0.6,cexCol = 1)
    

    Example dataset used-

    RC  C1  C2  C3  C4
    R1  1   1   0   1
    R2  0   1   1   0
    R3  0   1   1   1
    R4  1   1   1   0
    R5  1   1   1   1
    R6  0   0   0   1
    R7  1   1   1   1
    R8  1   1   1   1
    R9  0   1   1   1
    R10 1   1   0   0
    

    For a demo dataset