3

I've been working around with no success in solving how 2 or more pheatmaps (heatmaps) can be combined in a final plot.

data1 <- structure(list(DC1 = c(NA, NA, 1.98), DC2 = c(NA, NA, 0.14), 
    DC3 = c(1.85, 1.51, 0.52), DC4 = c(0.89, 0.7, 1.47), DC5 = c(0, 
    0.78, 0), DC6 = c(0, 1.3, 0), DC7 = c(0, 1.47, 0), DC8 = c(0, 
    1.2, 0), DC9 = c(0, 0, 0), DC10 = c(0.51, 1.9, 0)), .Names = c("DC1", 
    "DC2", "DC3", "DC4", "DC5", "DC6", "DC7", "DC8", "DC9", "DC10"), 
    enter code here`class = "data.frame", row.names = c("A", "B",  "C"))

data 2 <- structure(list(DC1 = c(9.56, 1.87, 2.07, 1.87, 2.07, 1.35), DC2 = c(5.51, 1.13, 1.25, 1.13, 0.99, 0.45), DC3 = c(4.84, 1.17, 0.66, 1.17, 
0.34, 0.16), DC4 = c(4.18, 0.59, 0.05, 0.97, 0.43, 0.59), DC5 = c(3.26, 
0, 0.14, 0.31, 0.79, 0.63), DC6 = c(3.35, 0, 1.12, 0.05, 1.12, 
0), DC7 = c(4.18, 0.63, 1.27, 0.47, 1.27, 0), DC8 = c(4.37, 1.17, 
1.3, 1.17, 0, 0), DC9 = c(4.3, 1.13, 0, 1.13, 0, 0), DC10 = c(7.47, 
1.88, 0.71, 1.88, 0, 0)), .Names = c("DC1", "DC2", "DC3", "DC4", 
"DC5", "DC6", "DC7", "DC8", "DC9", "DC10"), class = "data.frame", row.names = c("TD6 vs SH", 
"TD6 vs SAP", "TD6 vs NEA", "SH vs SAP", "SH vs NEA", "SAP vs NEA"
))

I construct very easily a heatmap using pheatmap by using these two codes:

hm_data1 <- pheatmap(as.matrix(data1))
hm_data2 <- pheatmap(as.matrix(data2))

However, in no way I can get both printed in one figure. I would like to see both of them horizontally. However, my real figure will be composed by 16 pheatmaps, so they must be arrange in 4 columns and 4 rows.

I tried with par mfrow with no success.

How can I combine pheatmaps?

I know there are plenty of R packages that can plot heatmaps, but I would like to do it with pheatmap

antecessor
  • 2,688
  • 6
  • 29
  • 61
  • This is because `pheatmap` appears to be using `grid` graphics. All the `par` commands only work with base plotting. `gridExtra::grid.arrange(hm_data1, hm_data2)` should do the trick. – Axeman Aug 01 '18 at 09:26
  • 2
    @Axeman I get this error: `only 'grobs' allowed in "gList"` – antecessor Aug 01 '18 at 09:49

1 Answers1

5

This will work.

library(gridExtra); library(pheatmap)

m <- matrix(c(1:4), ncol=2)
n <- matrix(c(1,1,1,2), ncol=2)
a <- list(pheatmap(m)[[4]])
a[[2]] <- pheatmap(n)[[4]]
z <- do.call(grid.arrange,a)
plot(z)

Based on one of the comments. If you have many single plots; you can use a loop like this.

mn <- list(m, n)

a <- list()
for(i in 1:length(mn)){
  a[i] <- list(pheatmap(mn[[i]])[[4]])
}

z <- do.call(grid.arrange,a)
plot(z)

The point is it to add all the data for your single plots in a list. You can then loop over the list, applying pheatmap.

milan
  • 4,782
  • 2
  • 21
  • 39
  • thanks for your comment. I do not want to generate a pdf, I would like to plot it in RStudio. Otherwise, which would be the code for 16 pheatmaps (4x4)? – antecessor Aug 01 '18 at 09:45
  • Please see the changed made. – milan Aug 01 '18 at 10:39
  • I got to plot two pheatmaps. However, when trying to plot more maps, it seems to be more tricky to me. How could I plot around 16 plots? I mean, I get lost with the use of [[4]]. I tried 16 instead of 4, creating 16 new lines with consecutive numbers after `a[[]]`, with no result. Any help? – antecessor Aug 01 '18 at 11:03
  • Yes, you should put the data that you want to plot in a list (in your case a single list with 16 matrices). Then do a simple for loop; every time applying the pheatmap function and adding the output to a new list (a in my example). You cannot change the '[[4]]' because that just says which part of the pheatmap output is needed for plotting later. – milan Aug 01 '18 at 11:25
  • Alright. I used this code with no success_ a <- list(mapa_calor(overlap_ALL_ODS_UI1), mapa_calor(overlap_ALL_ODS_LC), mapa_calor(overlap_ALL_ODS_LP3), mapa_calor(overlap_ALL_ODS_LM1), mapa_calor(overlap_ALL_ODS_LM3)) for(map in a) { z <- do.call(grid.arrange, map) plot(z) } – antecessor Aug 01 '18 at 12:26
  • 1
    I have added this code to the answer. Could you please check of this does the trick? – milan Aug 01 '18 at 12:36
  • That perfect. Thanks. – antecessor Aug 01 '18 at 12:41