我想在 for 循环中生成几个格子图,但它确实会创建空图像!!!
for (f in unique(df$month)) {
plot.new()
bwplot(x ~ country|type, data = df[df$month == f,], panel=function(...) {
panel.abline(h=0, col="green")
panel.bwplot(...)
})
savePlot(paste0("file_", f), "png")
}
当我“手动”运行 for 循环的内部时,它可以工作,但在循环中它会停止工作。为什么?
下面是生成数据的代码:
set.seed(123)
n <- 300
country <- sample(c("Europe", "Africa", "Asia", "Australia"), n, replace = TRUE)
type <- sample(c("city", "river", "village"), n, replace = TRUE)
month <- sample(c("may", "june", "july"), n, replace = TRUE)
x <- rnorm(n)
df <- data.frame(x, country, type, month)