4
votes

I am producing heatmaps with heatmap.2. I know how to controls many of the parameters but still I have not found a way of making the key of color only wider or putting it as a strip in a side or bottom of the plot.

With keysize it modifies both height and width proportionally.

Also when using ColSideColors I am using legend() to put the color labels, but 'topright' is not at the top-right. I know that this is something about the plot area, margins etc, but I have not found yet a good explanatory text of how heatmap.2 plot is structured and how to positioned things by coordinates and how to deal with oma, mar etc. Depending on the margins, samples, tree depth, etc. the legend could be placed in an open area or overlaps a bit of the heatmap. Any point to good texts for understanding theses issues with R graphics would be truly appreciated.

The coded used is:

df<- data.frame(  x1=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x2=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x3=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x4=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x5=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x6=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x7=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,x8=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
                  ,y1=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y2=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y3=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y4=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y5=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y6=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y7=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
                  ,y8=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
)
dataMatrix <- as.matrix(df)[sample(1:120),]
heatmap.2(dataMatrix
          , col=rev(brewer.pal(11,"RdBu"))
          , density.info="none"
          , key=TRUE
          , symkey=FALSE
          , trace="none"
          , cexRow=1
          , scale='row'
          , margins =c(10,9)
          , ColSideColors=c(rep("red", ncol(df)/2), rep("green", ncol(df)/2))
          , main="Log2_intensities median centered"
          , keysize=0.9)

legend('topright', c("x", "y"),lty=1, col=c("red", "green"), cex=0.8)
2
I know that heatplus can do some of these things, but at the moment all the code is done using both heatmap() and heatmap.2() and I can not change it right now.Pablo Marin-Garcia
If you insist on using heatmap.2, you're forced to accept putting your legend in the upper left corner. There may be other ways to draw legends, but those are outside of heatmap.2. Im unsure of what exactly you're willing to change to get different output. If you were looking for a simple parameter to pass to the function, that's not going to happen.MrFlick

2 Answers

3
votes

There is the way to organise your plot described here: Moving color key in R heatmap.2 (function of gplots package) When having a ColSideColors, heatmap.2 will draw the plot as

1 ColSideColors

2 heat map

3 ... so on as in the link above

Never tested with the RowSideColors. In the other words you can organise the plot using lmat, lwid, lhei parameters of heatmap.2. this could give you some space for your legend.

2
votes

You could try adding the 'inset' parameter and playing with the sizes to move the legend further right (first value) and/or further towards the top (second value) e.g:

legend('topright', inset = c(.02,.02), etc...)