Error in heatmap.2 in R gplots

Peeyush Sahu picture Peeyush Sahu · May 29, 2015 · Viewed 8.1k times · Source

I am using heatmap.2 in gplots to create heatmaps in R. My code used to work fine for creating a heatmap. But recently it started throwing errors. I am not sure why it is happening.

# Matrix to feed heatmap.2
mat = as.matrix(sort_pkmat)
# color palette for diff. colors
my_palette <- colorRampPalette(c("white","red","red4"))(n = 299)
# color breaks for range
col_breaks = c(seq(0,40,length=100),
               seq(40,60,length=100),
               seq(60,90,length=100))

path1 = paste(path,name,'.png', sep = '')
print(path1)
png(path1, 
    width = 10*300,        # 5 x 300 pixels
    height = 8*300,
    res = 300,            # 300 pixels per inch
    pointsize = 8)


heatmap.2(mat, 
          #cellnote = mat,  # same data set for cell labels
          main = "Tag_density_HeatMap", # heat map title
          xlab = "Peaks",
          ylab = "Chip_samples",
          labRow = FALSE,
          labCol = FALSE,
          cexRow = 0.7,           # Changes the size of col and row font size
          cexCol = 0.2,
          notecol="black",      # change font color of cell labels to black
          density.info="none",  # turns off density plot inside color legend
          trace="none",         # turns off trace lines inside the heat map
          margins =c(3,5),     # widens margins around plot
          col=my_palette,       # use on color palette defined earlier
          breaks=col_breaks,    # enable color transition at specified limits
          dendrogram="none",     # only draw a row dendrogram
          Colv= FALSE,           #cluster column
          Rowv = FALSE,
          #keysize = 1
)
dev.off()

Now it throws error:

Error in seq.default(min.raw, max.raw, by = min(diff(breaks)/4)) : 
  invalid (to - from)/by in seq(.)
> dev.off()
null device 
          1 

This code plots the heatmap but not the color key.

Answer

Benjamin Vincent picture Benjamin Vincent · Jun 2, 2015

I had this same problem using heatmap.2. It seems to result from having one or more zero values in the vector returned by diff(breaks). The 'by' call fails when min(diff(breaks)) is zero. I changed my break points to be non-overlapping and the color key was able to be displayed.

Initially I had: breaks=c(seq(0,0.033,length=25),seq(0.033,0.066,length=26),seq(0.066,0.1,length=25))

I changed this to: breaks=c(seq(0,0.033,length=25),seq(0.0331,0.066,length=26),seq(0.0661,0.1,length=25))

It seems to have resolved the issue and allows me to use the color key again.