热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何将pheatmap绘图绘制到屏幕并保存到文件-Howtodrawpheatmapplottoscreenandalsosavetofile

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 个解决方案

#1


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函数可以更容易地将最后绘制的绘图保存到文件中。

#2


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)

}

}

#3


0  

The wplot_save_this() function in the MarkdownReports saves any displayed plot to a .pdf file. Also, all other plotting functions in the package (wbarplot (), whist(), wplot(), etc ) display and save respective as .pdf automatically as well as linking them to a Markdown notebook/ report.

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


推荐阅读
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 先看看ElementUI里关于el-table的template数据结构:<template><el-table:datatableData><e ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
  • 本文介绍了关系型数据库和NoSQL数据库的概念和特点,列举了主流的关系型数据库和NoSQL数据库,同时描述了它们在新闻、电商抢购信息和微博热点信息等场景中的应用。此外,还提供了MySQL配置文件的相关内容。 ... [详细]
author-avatar
此情为谁伤
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有