【问题标题】:R: print multiple types of plots on one pageR:在一页上打印多种类型的图
【发布时间】:2015-11-09 15:26:49
【问题描述】:

我正在尝试在一页中绘制多个图。我知道像gridExtra::grid.arrange 这样的函数可以绘制由ggplot2 包生成的图形。我面临的问题是我有两个由ggplot2 包生成的图(bar.plot 和下面的density.plot)和一个使用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 plots可以用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()

任何帮助将不胜感激!

【问题讨论】:

  • @Roland 查看我的更新
  • 我没有看到你在哪里使用 gridBase 包中的函数。
  • 你给我的链接是gridBase的手册。我看到有一些使用pushViewport 的建议,我使用了它。
  • 由于需要组合多种绘图类型(基于网格的或 Excel、plotrix 等),我已将它们全部保存为。 .png 文件,使用 grid 包的 read.PNG 函数加载它们,然后使用 rasterGrob 函数对其进行转换。一旦所有图的格式都具有可比性,grid.arrange() 就可以了。

标签: r plot ggplot2 venn-diagram


【解决方案1】:

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

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

之后,使用g1 &lt;- 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

【讨论】:

    【解决方案2】:

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

    在下面,我绘制了两个 ggplot 和一个维恩图(取自 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      相关资源
      最近更新 更多