Reputation: 497
I have a list of plots and am using marrangeGrob
to write multiple plots per page. This gives me 6 plots per page:
marrangeGrob(plots, nrow=3, ncol=2, top=NULL)
But what I need is 5 plots per page (i.e. bottom right plot area is left blank)
Here is a reproducible example based on a subset of my data:
> 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()
How can I adjust this code for writing each of the 5 "Names" to a single page for each "VarName"?
Upvotes: 3
Views: 1325
Reputation: 77116
This version is slightly more general
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)
Upvotes: 0
Reputation: 77116
I've added the option to pass a layout, let me know if it breaks,
# 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))
Upvotes: 6
Reputation: 263451
Insert a blank grob in the 6th and 12th positions:
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()
Upvotes: 3