Question: Heatmap.2 giving specific color to a value and start gradient after
1
gravatar for Seigfried
20 months ago by
Seigfried70
Seigfried70 wrote:

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()
heatmap heatmap.2 R • 5.0k views
ADD COMMENTlink modified 20 months ago by e.rempel740 • written 20 months ago by Seigfried70
3
gravatar for e.rempel
20 months ago by
e.rempel740
Germany, Heidelberg, COS
e.rempel740 wrote:

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 COMMENTlink written 20 months ago by e.rempel740

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

ADD REPLYlink written 19 months ago by Seigfried70
0
gravatar for Samuel Brady
20 months ago by
Samuel Brady280
Samuel Brady280 wrote:

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 COMMENTlink written 20 months ago by Samuel Brady280
0
gravatar for Seigfried
20 months ago by
Seigfried70
Seigfried70 wrote:

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 COMMENTlink written 20 months ago by Seigfried70

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

ADD REPLYlink written 20 months ago by h.mon24k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 775 users visited in the last hour