gpt4 book ai didi

r - 使用 marrangeGrob 每页的奇数图

转载 作者:行者123 更新时间:2023-12-04 11:14:40 26 4
gpt4 key购买 nike

我有一个绘图列表,并且正在使用 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 个“名称”中的每一个写入每个“VarName”的单个页面?

最佳答案

我有 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

关于r - 使用 marrangeGrob 每页的奇数图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com