在 R 中使用 ggplot 对插图进行双循环

Double looping for inset plots using ggplot in R

我正在尝试 运行 循环以下代码:

library(tidyverse)
library(cowplot)

#Setup of Variables
N <- 199
K <- N+1
x <- rep(0,N)
x[1] <- 0.5
g <- 3.84
time <- c(1:K)

for (t in 1:N){
  x[t+1] = g*x[t]*(1-x[t])
}

A <- data.frame(time,x)

#create separate plots 1 & 2 --> combine into plot3 using ggdraw()
plot1 <- ggplot(data=A, aes(x=time, y=x)) + geom_line(color="blue") + xlim(0,150) + ylab("Fish Stock") + theme_classic()

plot2 <- ggplot(data=A, aes(x=time, y=x)) + geom_line(color="forestgreen") + xlim(8,15) + theme(legend.position="none", axis.title.x=element_blank(), axis.title.y=element_blank(), panel.border=element_blank(), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), axis.line=element_line(colour="black"), panel.background=element_rect(fill="white",colour="white"))

plot3 <- 
  ggdraw() +
  draw_plot(plot1) + 
  draw_plot(plot2, .68, .68, .3, .3) #insetplot, x, y, width, height

plot3

创建以下图形:

我想用这个做的是,将这个循环嵌套在另一个循环中,循环遍历 g 的值向量。我对这段代码的尝试如下:

G <- c(2.7, 2.9, 3.0, 3.5, 3.82, 3.83, 3.84, 3.85)

#Loop --> creation of x, dataframe, insetplots
for (g in G) {
  for (t in 1:N){
    x[t+1] = g*x[t]*(1-x[t])
  }

  A <- data.frame(time,x)

  plot1 <- ggplot(data=A, aes(x=time, y=x)) + geom_line(color="blue") + xlim(0,150) + ylab("Fish Stock") + theme_classic()
  plot2 <- ggplot(data=A, aes(x=time, y=x)) + geom_line(color="forestgreen") + xlim(8,15) + theme(legend.position="none", axis.title.x=element_blank(), axis.title.y=element_blank(), panel.border=element_blank(), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), axis.line=element_line(colour="black"), panel.background=element_rect(fill="white",colour="white"))
  plot3 <- 
  ggdraw() +
  draw_plot(plot1) + 
  draw_plot(plot2, .68, .68, .3, .3) #insetplot, x, y, width, height

  png(file = paste0("Fish-Stock-Inset_", g, ".png"))
  plot3
  dev.off()
}

但是,我得到了一系列空白图像。当我得到正确命名的文件时,绘图的保存是正确的,但图表本身丢失了。我几乎可以肯定问题出在我在初始 for (t in 1:N) 循环之后放置 A <- data.frame(time,x)

很抱歉,这只是一个调试问题,但我希望它能对其他人有所帮助运行遇到同样的问题。

出现问题是因为 plot3 仅在 for 循环结束时求值(在 R 中称为惰性求值)。

在下面尝试这个,我使用 ggsave 渲染绘图并注意在循环内声明你的 x:

G <- c(2.7, 2.9, 3.0, 3.5, 3.82, 3.83, 3.84, 3.85)

#Loop --> creation of x, dataframe, insetplots
for (g in G) {
  x <- rep(0,N)
  x[1] <- 0.5
  for (t in 1:N){
    x[t+1] = g*x[t]*(1-x[t])
  }

  A <- data.frame(time,x)

  plot1 <- ggplot(data=A, aes(x=time, y=x)) + geom_line(color="blue") + xlim(0,150) + ylab("Fish Stock") + theme_classic()
  plot2 <- ggplot(data=A, aes(x=time, y=x)) + geom_line(color="forestgreen") + xlim(8,15) + 
  theme(legend.position="none", axis.title.x=element_blank(), 
  axis.title.y=element_blank(), panel.border=element_blank(), 
  panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
  axis.line=element_line(colour="black"), 
  panel.background=element_rect(fill="white",colour="white"))
  plot3 <- 
    ggdraw() +
    draw_plot(plot1) + 
    draw_plot(plot2, .68, .68, .3, .3) #insetplot, x, y, width, height

  ggsave(plot3,file = paste0("Fish-Stock-Inset_", g, ".png"))
}