I have a tab separated text file as shown below

a   0.311   0.510   0.123   0.002   0.001   0.417   0.572   0.074   0.169
b   0.324   0.592   0.070   0.028   0.028   0.535   0.535   0.127   0.113

I am trying to use heatmap.2 function from gplots package and want to change the color key so that it ranges from 0 to 1

Following is my code:

d <- read.table("file.txt",sep="\t",header=F,row.names = 1)     
colnames(d) <- c("-3","-2","-1","+1","+2","+3","+4","+5","+6")
d_matrix <- data.matrix(d)
heatmap.2(d_matrix,scale="none",col = redblue(75),trace="none",Colv = FALSE,Rowv = FALSE,breaks=colors)

But this gives me error:

Error in image.default(1:nc, 1:nr, x, xlim = 0.5 + c(0, nc), ylim = 0.5 +  : 
  must have one more break than colour

I want to change to color key such that it ranges from 0 to 1. How can I do it in the function heatmap.2

I added breaks argument to the heatmap.2 function as suggested from this post here but no luck..


1 Answer 1


You just need to add more colors. The following, for example, will work:

heatmap.2(d_matrix, scale="none", trace="none", Rowv=F, Colv=F, breaks=seq(0,1,0.01), dendrogram="none", col=redblue(100))

Note that length(seq(0, 1, 0.01)) is 101, therefore redblue() must return one fewer (100) colors (or colours if you prefer extra vowels).

  • $\begingroup$ This worked. I have 2 more issues. I am having some sky blue lines in the color key which I don't want and secondly my rownames a and b are very big. Is there a way to reduce the size?? Thanks $\endgroup$ Commented Dec 15, 2017 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.