Question: how to plot multi pheatmap into one figure
gravatar for Zhilong Jia
4.2 years ago by
Zhilong Jia1.4k
Zhilong Jia1.4k wrote:

I'd want to combine 3 pheatmap into 1 figure. Here is the test code. it seems mfrow parameter does not work for it.


test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")

pheatmap(test, kmeans_k = 2)
pheatmap(test, kmeans_k = 3)
par pheatmap plotting • 7.2k views
ADD COMMENTlink modified 15 months ago by bitpir120 • written 4.2 years ago by Zhilong Jia1.4k

Stuff like this is why I had to write my own heatmap generating code (calls image()). I use layout() to set up multi-heatmap figures. It's pretty ugly.

ADD REPLYlink written 4.1 years ago by Madelaine Gogol5.0k

There's a little better of a description here about how to use layout() and image(). Multi-Panel Heatmaps In R

ADD REPLYlink written 4.0 years ago by Madelaine Gogol5.0k

Normally one just uses Adobe Illustrator or Inkscape to make the final figures. While you certainly can try to arrange everything in R, that's really not its forte.

ADD REPLYlink written 4.2 years ago by Devon Ryan88k

@Devon, Thanks. The Inkscape or others need plot this kind of figure manually but not automatically. That's the point.

ADD REPLYlink written 4.2 years ago by Zhilong Jia1.4k
gravatar for Jean-Karim Heriche
4.2 years ago by
EMBL Heidelberg, Germany
Jean-Karim Heriche18k wrote:

If it's just for the one figure, I'd do it manually in Inkscape. However, for high throughput figure production, I would export individual images from R and script the final figure assembly from the components, for example using ImageMagick. You could also write an SVG file directly (for example with the perl module SVG) or by scripting Inkscape.

ADD COMMENTlink written 4.2 years ago by Jean-Karim Heriche18k

why R can not do it ? it's strange. It usually works for other kind of plotting. I prefer using only one language if possible.

ADD REPLYlink written 4.2 years ago by Zhilong Jia1.4k

I am sure R can do it (have you tried ggplot2 ?) but it's likely unwieldy and not intuitive, unless of course, one is an R aficionado :) I find that R can generate simple plots quite easily but when it comes to getting to publication-quality figures (at least for biology journals), it's quickly getting more complicated. In line with the idea of assembling the components produced separately, you can output SVG files in R with the svg function then, since SVG is XML, you could also edit/combine these files in R.

ADD REPLYlink written 4.2 years ago by Jean-Karim Heriche18k

Agreed, in this case, one would need to directly modify the pheatmap source code. pheatmap directly specifies viewports to draw things in. I don't know enough about the internals of R graphics production, but I imagine that that would override anything set via users.

Sometimes R with a bit of external scripting really is the simplest route.

ADD REPLYlink written 4.2 years ago by Devon Ryan88k

A lot of heatmap functions use layout() and image() internally, so some of the things you specify via par() will be overridden. This is at least the case for heatmap.2() in the gplots library.

ADD REPLYlink written 4.2 years ago by Devon Ryan88k
gravatar for bitpir
15 months ago by
bitpir120 wrote:

Hi all, I know my answer is ~3years too late but I just want to put it out there and hopefully people will find this useful in the future. I found the answer here

Basically, user hrbrmstr mentioned that "The manual page for pheatmap() clearly states in the Values section that it returns a list of components, but it fails to mention component #4, which is the gtable of the plot. You have to index it specifically since it's not going to trigger R's print method lookup/execution for that object inside the function call."

For my case, I did the following:

for (a in items){
             cluster_rows = F,
             fontsize = 14,
             main = a)
  plot_list[[a]] = x[[4]]     ##to save each plot into a list. note the [[4]]
ADD COMMENTlink written 15 months ago by bitpir120

Instead of grid.arrange with, you could also use arrangeGrob. arrangeGrob gives you the chance to specify the layout (e.g. number of rows and columns for the combined plots.

g <- grid.arrange(arrangeGrob(grobs= plot_list,ncol=2))
ADD REPLYlink written 27 days ago by Yered10

Thank you for posting the answer. I can across it today, and it was very helpful for me.

ADD REPLYlink written 4 weeks ago by Yered10
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1103 users visited in the last hour