Heatmap.2 giving specific color to a value and start gradient after
3
1
Entering edit mode
7.4 years ago
Seigfried ▴ 80

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()
R Heatmap Heatmap.2 • 14k views
ADD COMMENT
3
Entering edit mode
7.4 years ago
e.rempel ★ 1.1k

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))
ADD COMMENT
0
Entering edit mode

Yes this is it! Didnt think of that. Thank you!

ADD REPLY
0
Entering edit mode
7.4 years ago
Samuel Brady ▴ 330

Are you certain that having too many NA values is really causing this error?

According to this post, you may have an infinite value:

https://stackoverflow.com/questions/23738900/error-while-creating-heatmaps-na-nan-inf-in-foreign-function-call-arg-11

ADD COMMENT
0
Entering edit mode
7.4 years ago
Seigfried ▴ 80

Hi Samuel

any( sapply(data, is.infinite) )
[1] FALSE
any( sapply(data, is.na) )
[1] TRUE
any( sapply(data, is.nan) )
[1] FALSE

There is no infinite value or NAN in the matrix. I thought that the hclust cannot plot the distance between samples because of too many NAs

ADD COMMENT
0
Entering edit mode

Do not answer here to an answer to your question, use the ADD COMMENT button at the appropriate answer.

ADD REPLY

Login before adding your answer.

Traffic: 2387 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6