如何在pdf的多个页面上绘制多个ggplot_gtable对象?

时间:2016-07-29 05:44:12

标签: r pdf ggplot2 gtable

虽然标题看起来很相似,但这不是一个重复的问题。

让我们说我有一个函数f1(),它绘制了一个类的对象" gTable,grob,gDesc"一旦。我使用grid.draw()绘制对象。

所以当我们打电话时:

pdf("filename.pdf", height = 10, weight =12)
f1()
dev.off()

这将提供一页pdf,这是我想要的。

然而,当我想通过代码在单个pdf中创建两个图:

pdf("filename.pdf")
f1()
f1()
dev.off()

我得到的是单页pdf,只有第二个情节。这是第一个情节被第二个覆盖。为了解决这个问题,我使用的是grid.newpage(),但在pdf中添加了一个额外的空白页面。怎么避免这个?只有设置了参数,我才能使用额外的参数来创建新页面。但是,我们还有其他选择吗?

我玩过grid.arrange()以及pdf()中的onefile参数,但没有任何效果。

此外,当我尝试在R绘图窗口上绘制这些图时,它会覆盖现有的打开图形设备。因此,在第二次绘图后,我无法使用后退箭头查看第一个图。 back arrow of R plot window

编辑:示例R代码,用于绘制类" gTable,grob,gDesc"

的对象
xxx <- function(){
set.seed(1111)
dd <- diamonds[sample(1:nrow(diamonds), 1000, replace = TRUE), ]
dd$color <- sample(letters[1:2], 1000, replace = TRUE)

p  <- ggplot(data = dd, aes(x = cut))  
p1 <- p + geom_bar(fill = "orange", alpha = 1) + facet_wrap(~color)+
          ggtitle("Main title")+scale_y_continuous("frequency", expand = c(0, 0))+
          labs(x = "cut", y = "frequency")+
          theme(panel.background = element_rect(colour = "white"),
                axis.text.x = element_text(angle = 45, hjust = 1),
                panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
               legend.position = "bottom") 

  p2 <- p + geom_line(aes(y = price), alpha = 0)+
            labs(x = "", y = "price")+expand_limits(y = 0) +
            stat_summary(aes(y = price, group = 1, colour = "mean"), fun.y = "mean", geom = c("point"))+ 
            stat_summary(aes(y = price, group = 1, colour = "mean"), fun.y = "mean", geom = c("line"))+ 
            stat_summary(aes(y = price, group = 1, colour = "median"), fun.y = "median", geom = "point")+
            stat_summary(aes(y = price, group = 1, colour = "median"), fun.y = "median", geom = "line")+
            scale_colour_manual(name = "" ,breaks = c("mean", "median"), values = c("red", "blue"))+
            facet_wrap(~color)+ylab("Exposures")+
            theme(panel.background = element_rect(fill = NA, colour = "white"),
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.border = element_rect(fill = NA, colour = "grey50"),
                  legend.position = "bottom") 

  xx <- ggplot_build(p1)
  yy <- ggplot_build(p2)

  nrow <- length(unique(xx$panel$layout$ROW))
  ncol <- length(unique(xx$panel$layout$COL))
  npanel <- length(xx$panel$layout$PANEL)

  g1 <- ggplot_gtable(xx)
  g2 <- ggplot_gtable(yy)

  pp <- c(subset(g1$layout, grepl("panel", g1$layout$name), se = t:r))
  g <- gtable_add_grob(g1, g2$grobs[grepl("panel", g1$layout$name)], 
                       pp$t, pp$l, pp$b, pp$l)

  func1 <- function(grob){
        widths <- grob$widths
        grob$widths[1] <- widths[3]
        grob$widths[3] <- widths[1]
        grob$vp[[1]]$layout$widths[1] <- widths[3]
        grob$vp[[1]]$layout$widths[3] <- widths[1]

        grob$children[[1]]$hjust <- 1 - grob$children[[1]]$hjust 
        grob$children[[1]]$vjust <- 1 - grob$children[[1]]$vjust 
        grob$children[[1]]$x <- unit(1, "npc") - grob$children[[1]]$x
        grob
     }


  index <- which(g2$layout$name == "ylab") 
  ylab <- g2$grobs[[index]]                # Extract that grob
  ylab <- func1(ylab) 
  ylab$children[[1]]$rot <- ylab$children[[1]]$rot + 180
  g <- gtable_add_cols(g, g2$widths[g2$layout[index, ]$l], pos = max(pp$r))
  g <-gtable_add_grob(g,ylab, t = min(pp$t), l = max(pp$r)+1, 
                      b = max(pp$b), r = max(pp$r)+1,
                      clip = "off", name = "2ndylab")

  j = 1
  k = 0

  for(i in 1:npanel){ 
    if ((i %% ncol == 0) || (i == npanel)){
      k = k + 1
      # swap the 2nd y-axis label

     index <- which(g2$layout$name == "axis_l-1")  # Which grob
     yaxis <- g2$grobs[[index]]                    # Extract the grob
     ticks <- yaxis$children[[2]]
      ticks$widths <- rev(ticks$widths)
     ticks$grobs <- rev(ticks$grobs)
     ticks$grobs[[1]]$x <- ticks$grobs[[1]]$x - unit(1, "npc")
     ticks$grobs[[2]] <- func1(ticks$grobs[[2]])
     yaxis$children[[2]] <- ticks
     if ((k == 1) || ((i == npanel) & (i%%ncol != 0)))#to ensure just once d secondary axisis printed 
      g <- gtable_add_cols(g,g2$widths[g2$layout[index,]$l], max(pp$r[j:i]))
      g <- gtable_add_grob(g,yaxis,max(pp$t[j:i]),max(pp$r[j:i])+1, max(pp$b[j:i])
                       , max(pp$r[j:i]) + 1, clip = "off", name = "2ndaxis")
     j = i + 1
  }
}

 pp <- c(subset(g2$layout, name == "guide-box", se = t:r))
 g <- gtable_add_grob(g, g2$grobs[[which(g2$layout$name == "guide-box")]], t = pp$t, 
                   l = pp$r, b = pp$b, r = pp$r )
 grid.draw(g)
}

这是一个示例函数。所以如果我调用以下代码:

pdf("zzz.pdf")
xxx()
xxx()
dev.off()

在上述情况下,只创建了一页pdf。如果你只是打电话

xxx()
xxx()

然后在R绘图窗口中,我只能查看第二个图。由于第一个图被覆盖,后面的箭头按钮被禁用。

1 个答案:

答案 0 :(得分:1)

解决方案是在第一个图之后使用grid.newpage()

 xxx <- function() {gg <- ggplotGrob(ggplot()); grid.draw(gg)}

 pdf("zzz.pdf")
 xxx()
 grid.newpage()
 xxx()
 dev.off()

enter image description here

您可能会发现让函数返回grob而不是绘制它更容易,并为要显示的此类对象的列表定义打印/绘制方法。有关此类策略,请参阅gridExtra:::print.arrangelist

xxx <- function() {gg <- ggplotGrob(ggplot()); gg}

plots <- marrangeGrob(replicate(3, xxx(), simplify = FALSE), nrow=1, ncol=1)
pdf("zzz.pdf")
print(plots)
dev.off()

enter image description here