R:在一页上打印多种类型的图 [英] R: print multiple types of plots on one page

查看:153
本文介绍了R:在一页上打印多种类型的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个页面上绘制多个图。我知道像 gridExtra :: grid.arrange 这样的函数可以绘制由 ggplot2 包生成的图形。我面临的问题是我生成了两个图( bar.plot density.plot )由 ggplot2 包和一个使用 limma :: vennDiagram 函数生成的图。我已经尝试了下面的内容,但它不起作用:

 输出<  -  paste('summary.pdf')$ b $ (b.pdf)
plot(density.plot)$ b(输出,宽度= 25,高度= 20)
par(mfrow = c(3,3))
$ b print(vennDiagram(dat.venn,circle.col = col,cex = c(3,3,3)))
不可见(dev.off())



dat.venn是VennCounts类型的数据:

  IH-10003-T1-D1 IH-10003-T2-D1计数
0 0 0 0
0 0 1 41
0 1 0 81
0 1 1 66
1 0 0 10
1 0 1 2
1 1 0 4
1 1 1 56
attr(,class)
[1]VennCounts

我无法找到与网格兼容的维恩图包.arrange 函数。我不认为使用 grid.arrange 函数和 ggplot2不能打印出 VennCounts 可以用 par 函数打印图。



更新:
我尝试使用pushViewport,但它仍然在下一页打印维恩图:

  pdf(输出,宽度= 25,高度= 20)

#创建布局:nrow = 2,ncol = 2
pushViewport(viewport(layout = grid.layout(2,2)))

#一个辅助函数来定义布局上的区域
define_region< - function(row,col){
viewport(layout.pos.row = row,layout.pos.col = col)


print(bar.plot,vp = define_region(1,1:2))
print(density.plot,vp = define_region( 2,1))
print(vennDiagram(dat.venn,circle.col = col,cex = c(3,3,3)),vp = define_region(2,2))
dev。 off()

任何帮助都会非常感谢!

解决方案首先,将每个文件保存为.png文件,例如 save(yourfile,file =yourname.png)。第二,使用grid包的read.PNG函数加载它们,比如 yours.png< - readPNG(yourfile。 PNG)



然后,使用rasterGrob函数将它们转换为 g1 < - rasterGrob(您的。 png,interpolate = TRUE)

一旦所有图形在格式上都可以比较, grid.arrange()就可以实现。它可能看起来像这样:

$ $ p $ $ $ $ $ $ grid.arrange(g1,g2,g3,nrow = 1)#one row
dev.copy(png,'threeplots.png')#保存图形数组
dev.off()#关闭设备


I am trying to plot multiple plots in one page. I know functions like gridExtra::grid.arrange that can plot graphs generated by ggplot2 package. The problem I am facing is that I have two plots (bar.plot and density.plot below) that are generated by ggplot2 package and one plot generated using limma::vennDiagram function. I have tried the below but it is not working:

output <- paste('summary.pdf')
pdf(output,width = 25,height = 20)
par(mfrow = c(3, 3))
plot(bar.plot)
plot(density.plot)
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)))
invisible(dev.off())

dat.venn is a data of type VennCounts:

 I-H-10003-T1-D1 I-H-10003-T2-D1 I-H-10003-T3-D1 Counts
               0               0               0      0
               0               0               1     41
               0               1               0     81
               0               1               1     66
               1               0               0     10
               1               0               1      2
               1               1               0      4
               1               1               1     56
attr(,"class")
[1] "VennCounts"

I am unable to find a venn diagram package that is compatible with the grid.arrange function. I don't think that VennCounts cant be printed out with grid.arrange function and ggplot2 plots can be printed out with par function.

UPDATE: I tried using pushViewport but it is still printing the venn diagram on the next page:

pdf(output,width = 25,height = 20)

# Create layout : nrow = 2, ncol = 2
pushViewport(viewport(layout = grid.layout(2, 2)))

# A helper function to define a region on the layout
define_region <- function(row, col){
  viewport(layout.pos.row = row, layout.pos.col = col)
} 

# Arrange the plots
print(bar.plot, vp = define_region(1, 1:2))
print(density.plot, vp = define_region(2, 1))
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)), vp = define_region(2, 2))
dev.off()

Any help would be much appreciated!

解决方案

First, save each of the files as .png files, such as save(yourfile, file = "yourname.png").

Second, use the read.PNG function of the grid package to load them, such as yours.png <- readPNG("yourfile.PNG")

After that, convert them with the rasterGrob function as in g1 <- rasterGrob(yours.png, interpolate=TRUE).

Once all the plots are comparable in format, grid.arrange() does the trick. It might look like this:

grid.arrange(g1, g2, g3, nrow=1)  # one row
dev.copy(png,'threeplots.png')   # to save the array of plots     
dev.off()                         # to close the device

这篇关于R:在一页上打印多种类型的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆