R:在一页上打印多种类型的图

R: print multiple types of plots on one page

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

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

我找不到与 grid.arrange 函数兼容的维恩图包。我不认为 VennCounts 不能用 grid.arrange 函数打印出来,ggplot2 图可以用 par 函数打印出来。

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

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

如有任何帮助,我们将不胜感激!

首先,将每个文件保存为.png文件,如save(yourfile, file = "yourname.png")

其次,使用grid包的read.PNG函数加载它们,如yours.png <- readPNG("yourfile.PNG")

之后,用g1 <- rasterGrob(yours.png, interpolate=TRUE)中的rasterGrob函数转换它们。

一旦所有图表在格式上都具有可比性,grid.arrange() 就可以了。它可能看起来像这样:

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

gridGraphics 包,如 gridBase,使得网格、ggplot、点阵和基础图形的混合非常简单。 grid.echo() 函数将基本图形转换为网格图形。 (使用 gridGraphics 的优点是基础图形可以通过 grid 进行编辑和修改;参见 gridGraphics.pdf。)

在下面,我画了两个 ggplots 和一个维恩图(取自 limma 包中的 vennDiagram 帮助页面),我将这三个图放置在视口中,确保维恩图图经过 grid.echo 处理。

更新到 ggplot2 2.0.0geom_bar 的默认 statstat = "count"

  library(ggplot2)
  library(gridGraphics)
  library(grid)
  library(limma)  # From bioC software repository

  grid.newpage()

  # Position 1st ggplot
  pushViewport(viewport(y = 1, height = 1/3, just = "top"))
  gg1 <- ggplot(mtcars, aes(x = mpg)) + geom_density()  
  print(gg1, newpage = FALSE)
  upViewport()

  # Position 2nd ggplot
  pushViewport(viewport(y = .5, height = 1/3, just = "centre"))
  gg2 <- ggplot(mtcars, aes(x = factor(carb, levels = 1:8))) + 
          geom_bar() + scale_x_discrete(drop = FALSE)
  print(gg2, newpage = FALSE)
  upViewport()

  # Function to draw venn diagram - the venn diagram is taken from ?vennDiagram
  plotfun <- function() {   
   Y <- matrix(rnorm(100*6),100,6)
   Y[1:10,3:4] <- Y[1:10,3:4]+3
   Y[1:20,5:6] <- Y[1:20,5:6]+3
   design <- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))
   fit <- eBayes(lmFit(Y,design))
   results <- decideTests(fit)
   a <- vennCounts(results)
   print(a)
   mfrow.old <- par()$mfrow
   par(mfrow=c(1,2))
   vennDiagram(a)
   vennDiagram(results, 
       include=c("up", "down"),
       counts.col=c("red", "blue"),
       circle.col = c("red", "blue", "green3"))
  }

  # Position the venn diagram
  pushViewport(viewport(y = 0, height = 1/3, just = "bottom"))
  grid.echo(plotfun, newpage = FALSE)
  upViewport(0)