1

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

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

这将创建以下图形:

在此处输入图像描述

我想要做的是将这个循环嵌套在另一个循环中,该循环通过值向量 for 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()
}

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

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

4

1 回答 1

1

出现您的问题是因为 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"))
}
于 2020-01-09T18:36:12.360 回答