Pablo Marin-Garcia
Pablo Marin-Garcia

Reputation: 4251

heatmap.2 (gplots) how to change horizontal size of the color key and add a legend

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)

Upvotes: 4

Views: 7027

Answers (2)

Gvaihir
Gvaihir

Reputation: 51

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.

Upvotes: 3

Rebecca Hannah
Rebecca Hannah

Reputation: 21

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

Upvotes: 2

Related Questions