4

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)
colors<-seq(0,1,length.out=100)
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..

terdon
  • 10,071
  • 5
  • 22
  • 48
user3138373
  • 420
  • 1
  • 5
  • 13

1 Answers1

4

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).

Devon Ryan
  • 19,602
  • 2
  • 29
  • 60
  • 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 – user3138373 Dec 15 '17 at 16:24