Question: Conditional Coloring Of Heatmaps
gravatar for Assa Yeroslaviz
8.0 years ago by
Assa Yeroslaviz1.4k
Assa Yeroslaviz1.4k wrote:

Hi all,

I am trying to create a heatmap for my data. Here is a short part of the matrix

                1       2       3          A          B          C         D
  1        8.1733  6.4126  6.2126 16.59719550 16.285273 16.1536943 16.226814
  2            NA      NA      NA 11.05089616 11.702181 10.6578633 10.607349
  3            NA      NA      NA  6.60984359  4.234195  3.6587827  6.665620
  4       13.2216 13.2145 13.2326 14.08834851 14.214714 14.6321250 12.590646
  5       14.3365 14.2590 14.3201 15.69146410 14.873010 13.6182272 15.692651
  6            NA      NA      NA  0.00000000  0.000000  0.0000000  1.310340
  7            NA      NA      NA  0.07038933  0.000000  0.2016339  0.000000

I am using the heatmap.2 command from the gplots package in R. What I would like to have is the color grey for the NA, the color white for 0 and the rst should follow a certain color palette I am creating.

This is what I am doing at the moment

pairs.breaks <- c(0, seq(1, 18.23145, length.out=30))
color.palette= rev(colorRampPalette(c("yellow","orange","red","black"))(length(pairs.breaks) - 2))

heatmap.2( matrix,
Rowv=FALSE,na.rm=F, na.color="grey",  Colv=FALSE, 
trace="none",# tracecol="gray50", margin=c(4,5),
dendrogram = "none", 
colsep=c(3), sepcolor="white",
lhei=c(1,12), lwid=c(2, 8),

The problem is, the in the last line for eaxmple the values 0.2016339 is also white. I would like white to be explicitly for 0 values. Anything else must have a color in my color-range.

Is it possible?

I would appreciate any help.


R heatmap • 9.9k views
ADD COMMENTlink modified 8.0 years ago by Biesterfeld30 • written 8.0 years ago by Assa Yeroslaviz1.4k

use ggplot2 for nice R-graphics (reference guide here). You can make heatmaps and map the values of a column in your dataframe to a continuous colour gradient, you can give NA-values a specific colour and in this post you can get advice on how to deal with outliers in your data range that mess up the scale of the heatmap coloring

ADD REPLYlink modified 8.0 years ago • written 8.0 years ago by Irsan7.2k
gravatar for Biesterfeld
8.0 years ago by
Biesterfeld30 wrote:

Your breaks look like

0.000000  1.000000  1.594188  2.188376

which means, that every value in [0,1) will be assigned by color white. Hence, you have to adjust your breaks:

matrix.min  <- min(matrix[ matrix!=0 ], na.rm=TRUE)
matrix.max <- max( matrix, na.rm = TRUE )
pairs.breaks <- c(0, seq( matrix.min, matrix.max, length.out=30) )
ADD COMMENTlink written 8.0 years ago by Biesterfeld30
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: 1283 users visited in the last hour