0

I am trying to display heatmaps next to each other in R, but it fails. What I am doing is:

library(gplots)
xPos11<-read.table("dataset1.txt")
xPos2<-read.table("dataset2.txt")
xPos3<-read.table("dataset3.txt")
xPos4<-read.table("dataset4.txt")
colorMin = 0 
colorMax = 5 
pdf("Region3.heatmap.pdf")
par(mfrow=c(4,4))
heatmap.2(as.matrix(   xPos1  ), Rowv=FALSE, Colv=FALSE, dendrogram="none", key=TRUE, density.info="none", trace="none", breaks=seq(colorMin, colorMax, length.out=101))
heatmap.2(as.matrix(   xPos2 ), Rowv=FALSE, Colv=FALSE, dendrogram="none", key=TRUE, density.info="none", trace="none", breaks=seq(colorMin, colorMax, length.out=101))
heatmap.2(as.matrix(   xPos3), Rowv=FALSE, Colv=FALSE, dendrogram="none", key=TRUE, density.info="none", trace="none", breaks=seq(colorMin, colorMax, length.out=101))
heatmap.2(as.matrix(   xPos4  ), Rowv=FALSE, Colv=FALSE, dendrogram="none", key=TRUE, density.info="none", trace="none", breaks=seq(colorMin, colorMax, length.out=101))
dev.off()

Now what is happening is instead of getting one pdf page with all 4 heatmaps next to echother I am getting 4 pages of the pdf with one heatmap on each of them.

Could someone help me in tweaking the code to get the desired structure panel of heatmaps in R.

Thank you

Angelo
  • 4,829
  • 7
  • 35
  • 56
  • 1
    The following link may help: http://stackoverflow.com/questions/13081310/combining-multiple-complex-plots-as-panels-in-a-single-figure – Ruthger Righart Feb 24 '15 at 19:45
  • `heatmap` and `heatmap.2` don't seem to respect either `mfrow` or `layout`. Another option is to use the `image` function to draw your heatmaps, and use `mfrow` or `layout` to position them in a multi-panel plot. See [here](https://www.biostars.org/p/86379/) for an example. – eipi10 Feb 24 '15 at 20:00

1 Answers1

0

You can plot the heatmaps to separate files and then combine them with the "montage" command from imagemagick:

m <- matrix(runif(10^2), ncol=10)
for (i1 in 1:4) {
    ifile <- paste0(i1,'_heatmap.pdf')
    pdf(ifile)
    heatmap(m)
    d <- dev.off()
}
system('montage -geometry 100% -tile 2x2 ./*_heatmap.pdf outfile.pdf')
chakalakka
  • 464
  • 2
  • 9