Question: Heatmap.2 giving specific color to a value and start gradient after
gravatar for Seigfried
3.2 years ago by
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())

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

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[] <- 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

Heatmap <- heatmap.2(as.matrix(data),
      main = "Heatmap of p-values for GO", = "none",
      trace = "none",
      margins = c(5,28),
      key.xlab = "log10 Values",
      cexRow = 1,
      cexCol = 1.5,
      col = color.palette,  # use on color palette defined earlier
      breaks=col_breaks,    # enable color transition at specified limits
      dendrogram ="both")
heatmap heatmap.2 R • 9.6k views
ADD COMMENTlink modified 3.2 years ago by e.rempel890 • written 3.2 years ago by Seigfried70
gravatar for e.rempel
3.2 years ago by
Germany, Heidelberg, COS
e.rempel890 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 3.2 years ago by e.rempel890

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

ADD REPLYlink written 3.2 years ago by Seigfried70
gravatar for Samuel Brady
3.2 years ago by
Samuel Brady320
Samuel Brady320 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:

ADD COMMENTlink written 3.2 years ago by Samuel Brady320
gravatar for Seigfried
3.2 years ago by
Seigfried70 wrote:

Hi Samuel

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

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 3.2 years 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 3.2 years ago by h.mon31k
Please log in to add an answer.


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