I am trying to plot a Heatmap. My data is as follows.

    GO	s_3d	s_6d	s_17d
    peptide metabolic process	4.71E-29	2.84828E-20	2.70719E-24
    organonitrogen compound biosynthetic process	3.38158E-22	3.26674E-15	6.3903E-22
    positive regulation of gene expression		0.057421474	
    negative regulation of RNA metabolic process		0.05933718	
    G-protein coupled receptor signaling pathway		0.061777665	
    positive regulation of macromolecule biosynthetic process		0.061906369	
    negative regulation of nitrogen compound metabolic process		0.065645467	

Basically the data has 4 columns and a LOT of NA values.
I am using the function Heatmap.2 which usually has an option called na.color which can help me color the NA values to a distinct color

Since I have too many NA values, the heatmap function gives me an error

    Error in hclustfun(distr) : NA/NaN/Inf in foreign function call (arg 11)

Therefore I tried to replace all NAs with zero and then give that a separate color. However the heatmap gives me a gradient even though I give the color separate. What I want is all 0's to be White and Everything which is not 0 to have a gradient from yellow to red. I do not want a gradient between white to yellow. How do I do this?

This is my code

    rm(list = ls())
    cat("\014") 

    if (!require("RColorBrewer")) {
    install.packages("RColorBrewer", dependencies = TRUE)
    library(RColorBrewer)
    }

    file <- "C:/Users/niran/Downloads/go_cluster.txt"   #input
    data <- read.table(file,header=T, sep = "\t",stringsAsFactors = F)
    row.names(data) <- data[,1]
    data <- data[-1]

    # Since data is spread too far apart we can do a log transform to reduce the gaps

    data[is.na(data)] <- 0 # Find possible solution for this
    data <- -log10(data + 0.000000000001)

    # creates a own color palette from yellow to red
    color.palette  <- colorRampPalette(c("#F8F8F8","yellow", "orange", "red"))(n=600)

    col_breaks = c(seq(0,0.1,length=1),  # for white
               seq(0.1,2,length=100),
               seq(2.01,4,length=100),
               seq(4.01,6,length=100),
               seq(6.01,8,length=100),
               seq(8.01,10,length=100),
               seq(10.01,12,length=100))

    Heatmap <- heatmap.2(as.matrix(data),
          main = "Heatmap of p-values for GO",
          density.info = "none",
          trace = "none",
          margins = c(5,28),
          key.xlab = "log10 Values",
          cexRow = 1,
          cexCol = 1.5,
          keysize=0.75,
          col = color.palette,  # use on color palette defined earlier
          breaks=col_breaks,    # enable color transition at specified limits
          dendrogram ="both")

    dev.off()


Hi Seigfried,

you give the separate colors, but **ColorRamPalette** still interpolates them to create a smooth gradient from white to yellow to orange to red. If I understood you correctly, you would like first create a smooth gradient from yellow to orange to red and **then** prepend "white".  Thus your code should contain something like:

    color.palette  <- c("#F8F8F8", colorRampPalette(c("yellow", "orange", "red"))(n=599))