Programming/R

[R] pheatmap 저장 방법

김해김씨99대손 2023. 6. 2. 14:11

ggsave로 저장이 안 되길래 뭐지? 하고 찾아봤는데 Rbase의 이미지 저장함수인 png, pdf ..를 사용해야 한다. 

원리는 잘 모르겠지만 외국인의 함수를 가져와 사용해 보자. 

 

 

 

 

library(pheatmap)

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 = "")
xx <- pheatmap(test)

 

 

 

 

pdf 버전 

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

 

png 버전 

save_pheatmap_png <- function(x, filename, width=7, height=7, res = 300, units = "in") {
   stopifnot(!missing(x))
   stopifnot(!missing(filename))
   png(filename, width=width, height=height, res = res, units = units)
   grid::grid.newpage()
   grid::grid.draw(x$gtable)
   dev.off()
}
save_pheatmap_png(xx, "./test.png")

 

 

| 출처

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

반응형