Search code examples
rgplots

R - gplots - Removing white space in heatmap.2 when key=FALSE


I have:

library(gplots);
x<-matrix(seq(1:100),nrow=10,byrow=TRUE);
heatmap.2(x, Rowv=NA, Colv=NA, scale="none", main="This title will be cut off by the white space where the non-existant key is supposed to go.", col=gray((255:0)/255), dendrogram="none",trace="none", key=FALSE);

When the key is specified as FALSE, there's a block of white-space on the left side of the plot that prevents the full title from showing up, conflicts with manual specification of smaller margins, and moves the heat-map toward the right. The width of the white-space is controllable using "keysize=#", but making it too small (somewhere between 0.8 and 1.0) creates an error: "Error in plot.new() : figure margins too large"

I would try doing this with heatmap() instead of heatmap.2(), but heatmap doesn't play well with par() which I need for a project. If anyone has any suggestions, I'd appreciate it.


Solution

  • Positioning elements of the heatmap.2 plot can be done using the layout parameter(s).

    layout(mat = lmat, widths = lwid, heights = lhei)
    

    I get a pretty acceptable heatmap plot using the following.

    heatmap.2(x, 
        Rowv=NA, 
        Colv=NA, 
        scale="none", 
        main="This title will be cut off by the white space where the non-existant key is supposed to go.", 
        col=gray((255:0)/255), 
        dendrogram="none",
        trace="none", 
        key=FALSE, 
        lmat=rbind(c(2),c(3),c(1),c(4)), 
        lhei=c(1,1,9,0), 
        lwid=c(1)
        );
    

    Please refer to ?layout or this answer on Stack Exchange for more details.