【问题标题】:How to draw pheatmap plot to screen and also save to file如何将 pheatmap 绘图绘制到屏幕并保存到文件
【发布时间】:2017-03-27 16:17:24
【问题描述】:

我正在使用 pheatmap 包。默认情况下,它将绘图绘制到屏幕上。就我而言,这意味着在 R studio 的 R markdown notebook 中输出。但我也想保存到文件中。如果我将它保存到一个文件中,并给它filename= 参数,它不会绘制到屏幕上(R 笔记本)。有没有办法让这两件事都发生?更一般地说,对于我想要保存和显示在屏幕上的任何绘图 (ggplot2)?

【问题讨论】:

    标签: r ggplot2 pheatmap


    【解决方案1】:

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

    我们可以渲染绘图并保存结果

    xx <- pheatmap(test)
    

    然后您可以通过打开图形设备并按照在主函数中完成的方式重新绘制结果来将其输出到文件

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

    此包直接使用网格库,不使用ggplot2,因此该包的解决方案会有所不同。 ggsave 函数可以更轻松地将最后绘制的图保存到文件中。

    【讨论】:

    • 谢谢!这非常有用。太糟糕了,这有点令人费解,但我还是会接受。
    【解决方案2】:

    仅供参考,我做了一个更复杂的函数,包括制作 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)
    

    }

    【讨论】:

      【解决方案3】:

      MarkdownReports 中的wplot_save_this() 函数将任何显示的绘图保存到.pdf 文件中。此外,包中的所有其他绘图功能 (wbarplot (), whist(), wplot(), etc) 会自动显示并保存为 .pdf,并将它们链接到 Markdown 笔记本/报告。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-08
        • 1970-01-01
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多