How to change xmax and xmin in histogram part of heatmap.2 plot from the R package gplots?

1.9k views Asked by At

I have multiple heat maps similar to the following:

X <- matrix(nrow=3, ncol=3)
X[1,] <- c(0.3, 0.4, 0.45)
X[2,] <- c(0.3, 0.7, 0.65)
X[3,] <- c(0.3, 0.4, 0.45)
colnames(X)<-c(1.5, 3, 4)
rownames(X)<-c(1.5, 3, 4)
library(gplots)

heatmap.2( X, Rowv=NA, Colv=NA, col=rev(heat.colors(256)), 
           sepcolor="black", trace="none",dendrogram="none" )

Now, in order to make multiple plots of this kind look more similar, how can I get the upper left histogram to always go between 0 and 1?


Based on yuk's answer I made this version:

X <- matrix(nrow=3, ncol=3)
X[1,] <- c(0.3, 0.4, 0.45)
X[2,] <- c(0.3, 0.7, 0.65)
X[3,] <- c(0.3, 0.4, 0.45)
colnames(X)<-c(1.5, 3, 4)
rownames(X)<-c(1.5, 3, 4)
library(gplots)

colors <- rev(heat.colors(256))

colbr <- c(seq(0, 1, len=length(colors)+1))
heatmap.2(X, scale="none", col=colors, breaks=colbr,
          key=T, symkey=F, density.info="histogram", trace="none", Rowv=NA, Colv=NA, 
          sepcolor="black", dendrogram="none" )

Now the color scale goes between 0 and 1 bit the histogram does still not.enter image description here

1

There are 1 answers

1
yuk On

I think the best way is to set color breaks.

Here is what i usually do (x is the matrix for heatmap):

n.col=16 # number of colors
cm = redblue(n.col) # red-white-blue colormap
mmx = min(abs(min(x)),abs(max(x))) # find min value, or you can set to a number
colbr <- c(seq(-mmx/2,mmx/2, len=length(cm)+1)) # vector of symmetric breaks
heatmap.2(x, scale="none", col=cm, breaks=colbr,
          key=T, symkey=F, density.info="histogram", trace="none")