marrangeGrob每页奇数个图

时间:2016-05-24 22:35:09

标签: r gridextra

我有一个图表列表,我使用marrangeGrob每页编写多个图表。这给了我每页6个图:

marrangeGrob(plots, nrow=3, ncol=2, top=NULL)

但我需要的是每页5个图(即右下方的绘图区域留空)

以下是基于我的数据子集的可重现示例:

> CohData
     Name ScenID ScenName Year VarName Units value
1  Site01      0     Hind 2000     ANC ueq/L 28.23
2  Site02      0     Hind 2000     ANC ueq/L 35.34
3  Site03      0     Hind 2000     ANC ueq/L 31.73
4  Site04      0     Hind 2000     ANC ueq/L 54.21
5  Site05      0     Hind 2000     ANC ueq/L 28.23
6  Site01      1       BC 2020     ANC ueq/L 45.01
7  Site02      1       BC 2020     ANC ueq/L 32.86
8  Site03      1       BC 2020     ANC ueq/L 30.38
9  Site04      1       BC 2020     ANC ueq/L 53.01
10 Site05      1       BC 2020     ANC ueq/L 31.11
11 Site01      0     Hind 2000      pH        6.40
12 Site02      0     Hind 2000      pH        5.93
13 Site03      0     Hind 2000      pH        6.36
14 Site04      0     Hind 2000      pH        5.75
15 Site05      0     Hind 2000      pH        5.16
16 Site01      1       BC 2020      pH        6.63
17 Site02      1       BC 2020      pH        5.86
18 Site03      1       BC 2020      pH        6.33
19 Site04      1       BC 2020      pH        5.73
20 Site05      1       BC 2020      pH        5.23

library(ggplot2)
library(grid)
library(gridExtra)

## Iterate plots and write to PDF
pdf("plots.pdf")
plots <- list()
CohData_nohind <- subset(CohData, ScenID > 0)
stream_vars <- list("ANC", "pH")
for (i in unique(CohData_nohind$ScenID)){
  subdata1 <- subset(CohData_nohind, ScenID == i)
  ScenName <- unique(subdata1$ScenName)
  subdata1 <- subset(CohData, ScenID == i | ScenID == 0)
  for (j in stream_vars){
    subdata2 <- subset(subdata1, VarName == j)
    units <- unique(subdata2$Units)
    for (k in unique(subdata2$Name)){
      subdata3 <- subset(subdata2, Name == k)

      if (j=='ANC'){
        p <- ggplot(subdata3, aes(x = Year, y = value)) + geom_line() + expand_limits(y=c(0,100)) + 
          ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) + ylab(paste0(j, " (", units, ")"))
        plots[[paste(i,j,k,sep="_")]] = p

      } else if (j=='pH'){
        p <- ggplot(subdata3, aes(x = Year, y = value)) + geom_line() + expand_limits(y=c(3,7)) +
          ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) + ylab(paste0(j, " (", units, ")"))
        plots[[paste(i,j,k,sep="_")]] = p
      }
    }
  }
}
interleave <- function(x, y, after = 1){

  nx <- length(x)
  ny <- length(y)

  if((nx %% after) > 0) x <- rep(x, length.out=after * ceiling(nx / after))
  mx <- matrix(x, ncol = after, byrow=TRUE)
  my <- rep(y, length.out = nrow(mx))

  c(t(cbind(mx, my)))
}

x <- replicate(7, ggplot(), simplify=FALSE)
y <- replicate(3, nullGrob(), simplify=FALSE)

marrangeGrob(interleave(x,y,3), ncol=2, nrow=2)
dev.off()

如何调整此代码以编写5&#34;名称&#34;每个&#34; VarName&#34;?

的单个页面

3 个答案:

答案 0 :(得分:6)

added the option传递布局,让我知道它是否破裂,

# devtools::install_github("baptiste/gridextra")
library(gridExtra)
library(grid)

gl <- lapply(seq_len(7), function(ii) grobTree(rectGrob(gp=gpar(fill=ii)), 
                                               textGrob(ii)))
marrangeGrob(gl, layout_matrix=matrix(c(1,2,3,NA), 2))

enter image description here

答案 1 :(得分:3)

在第6和第12位置插入空白grob:

plots[7:11] <- plots[6:10]
plots[[6]] <- textGrob("")
plots[[12]] <- textGrob("")

 pdf("plots.pdf")
 marrangeGrob(plots, nrow=3, ncol=2, top=NULL)
 dev.off()

enter image description here

答案 2 :(得分:0)

这个版本略胜一筹

interleave <- function(x, y, after = 1){

  nx <- length(x)
  ny <- length(y)

  if((nx %% after) > 0) x <- rep(x, length.out=after * ceiling(nx / after))
  mx <- matrix(x, ncol = after, byrow=TRUE)
  my <- rep(y, length.out = nrow(mx))

  c(t(cbind(mx, my)))
}

library(ggplot2)
library(grid)
library(gridExtra)
x <- replicate(7, ggplot(), simplify=FALSE)
y <- replicate(3, nullGrob(), simplify=FALSE)

marrangeGrob(interleave(x,y,3), ncol=2, nrow=2)