Conditional Coloring Of Heatmaps
1
2
Entering edit mode
11.5 years ago
Assa Yeroslaviz ★ 1.8k

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", 
col=c("white",color.palette),
colsep=c(3), sepcolor="white",
lhei=c(1,12), lwid=c(2, 8),
breaks=pairs.breaks)

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.

Assa

r heatmap • 12k views
ADD COMMENT
5
Entering edit mode

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 REPLY
0
Entering edit mode
11.4 years ago
Biesterfeld ▴ 30

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 COMMENT

Login before adding your answer.

Traffic: 2523 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