[R] to save pheatmap picture object file

Generally, we use images pheatmap obtained by Rstudio interaction can be exported in the Export plots, how to save the object to a file it? This requirement is common in automated processes, the author seems to have no explanation.

Generating sample data:

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 = "")

Look at the data sub-sub:
image.png

Implementation

Next implemented method in two steps:
1. Save Object

library(pheatmap)
xx <- pheatmap(test)

2. Open the graphics device to re-draw
the package using a grid graphics system rather than ggplot2, so the solution is different. Generated by a custom function, but also a plurality of graphic objects drawn at once.

save_pheatmap_pdf <- function(x, filename, width=7, height=7) {
   stopifnot(!missing(x))
   stopifnot(!missing(filename))
   pdf(filename, width=width, height=height)
   grid::grid.newpage()
   grid::grid.draw(x$gtable)
   dev.off()
}

save_pheatmap_pdf(xx, "test.pdf")

image.png

Ref: https://stackoverflow.com/questions/43051525/how-to-draw-pheatmap-plot-to-screen-and-also-save-to-file

Guess you like

Origin www.cnblogs.com/jessepeng/p/11361263.html