作者:此情为谁伤 | 来源:互联网 | 2023-01-19 16:26
Imworkingwiththepheatmappackage.Bydefault,itdrawstheplottothescreen.Inmycase,that
I'm working with the pheatmap package. By default, it draws the plot to the screen. In my case, that means output in a R markdown notebook in R studio. But I also want to save to a file. If I save it to a file, giving it the filename=
argument, it doesn't draw to the screen (R notebook). Is there a way to get both things to happen? And more generally, with any plot (ggplot2) where I want to both save and show on the screen?
我正在使用pheatmap包。默认情况下,它会将绘图绘制到屏幕上。就我而言,这意味着在R studio的R markdown笔记本中输出。但我也想保存到文件中。如果我将它保存到文件,给它filename =参数,它不会绘制到屏幕(R笔记本)。有没有办法让这两件事情发生?更一般地说,任何情节(ggplot2)我想在屏幕上保存和显示?
3 个解决方案
2
The authors of pheatmap didn't seem to make this super easy. But it's something you are going to need to do in two separate steps. First, we use the sample data from the ?pheatmap
help page
pheatmap的作者似乎并没有让这非常容易。但这是你需要分两步完成的事情。首先,我们使用来自?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 = "")
We can render the plot and save the result with
我们可以渲染绘图并保存结果
xx <- pheatmap(test)
Then you can output to this to a file by opening a graphics device and re-drawing the result the way it's done in the main function
然后你可以通过打开一个图形设备输出到这个文件,然后按照在main函数中完成的方式重新绘制结果
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")
This package uses the grid library directly and does not use ggplot2
so solutions for that package would be different. The ggsave
function makes it easier to save the last drawn plot to a file.
该软件包直接使用网格库,不使用ggplot2,因此该软件包的解决方案会有所不同。 ggsave函数可以更容易地将最后绘制的绘图保存到文件中。
0
FYI, I made a more complex function that includes making the pheatmap and then calling the save_heatmap
function from above. I'm posting it here if it is useful to anyone, and also for critiques. I added in a line to save the heatmap image file with the name of the matrix that gives rise to the heatmap. This is helpful for downstream organization of the files.
仅供参考,我做了一个更复杂的功能,包括制作pheatmap,然后从上面调用save_heatmap函数。如果它对任何人都有用,我也会在这里发布,也可以用于批评。我添加了一行来保存热图图像文件,其中包含产生热图的矩阵名称。这有助于下游组织文件。
save_pheatmap <- function(x, filename, width=480, height=960) {
stopifnot(!missing(x))
stopifnot(!missing(filename))
png(filename,width = width, height=height)
grid::grid.newpage()
grid::grid.draw(x$gtable)
dev.off()
}
plot_heatmap <- function(mat,color=NULL, cluster_rows=NULL, cluster_cols=NULL, scale=NULL,
cellwidth=NULL, cellheight=NULL,show_colnames=NULL, labels_col=NULL, show_rownames=NULL,
border_color=NULL,legend=NULL,...){
#Default Color
if (is.null(color)){
color=rev(col.pal)
}
#Default cluster
if (is.null(cluster_rows)){
cluster_rows=FALSE
}
if (is.null(cluster_cols)){
cluster_cols=FALSE
}
#Default sclae
if(is.null(scale)){
scale="none"
}
#Default cell dims
if (is.null(cellwidth)){
cellwidth=12
}
if (is.null(cellheight)){
cellheight=12
}
#Default Labels
if (is.null(show_colnames)){
show_colnames=TRUE
}
if (is.null(labels_col)){
labels_col=NULL
}
if (is.null(show_rownames)){
show_rownames=FALSE
}
#Set border
if (is.null(border_color)){
border_color=NA
}
#Legend
if (is.null(legend)){
legend=FALSE
}
temp_hm <- pheatmap(mat,color=color, cluster_rows=cluster_rows, cluster_cols=cluster_cols, scale=scale,
cellwidth=cellwidth, cellheight=cellheight,show_colnames=show_colnames, labels_col=labels_col,
show_rownames=show_rownames,border_color=border_color,legend=legend)
temp_hm_name <- paste(deparse(substitute(mat)),".png", sep="")
save_pheatmap(temp_hm, filename=temp_hm_name)
}
}