简体   繁体   English

R-多个for循环以生成和保存绘图

[英]R - Multiple for loop to produce and save plots

I am trying to produce several R plots (outputs of the "vegan" package) using several for loop condition to subset my dataset. 我正在尝试使用多个for循环条件生成我的数据集的多个R图(“纯素”包的输出)。 If I only use one for condition, I will retrieve the plots that I want but if I chain the for loops, it will not create the number of expected conditions. 如果仅将一个用于条件,则将检索所需的图,但是如果将for循环链接起来,则不会创建预期条件的数量。

My data frame is similar to: 我的数据框类似于:

my_df<- data.frame(Cruise = sample(c("cruise2016" ,"cruise2008" ,"cruise2012" ,"cruise2014" ,"cruise2011"), 50, replace=T),
                Depth_interval = sample(c("100","200","500"), 50, replace = T),
                data.frame(matrix(sample(1:100,300,replace=T), nrow=50)))

I use two lists as conditions for the loops: 我使用两个列表作为循环的条件:

cruise.list <- unique(my_df$Cruise)
interval.list <- unique(my_df$Depth_interval)

With these lists, I would like to plot all the depth intervals (3 uniques) within each cruise (5 uniques) 使用这些列表,我想绘制每个巡航中的所有深度间隔(3个唯一性)(5个唯一性)

for (i in seq_along(cruise.list)) {

  for (i in seq_along(interval.list)) {

    pdf(paste("my_path",
              cruise.list[i],
              interval.list[i],
              ".pdf", sep=""))

    df <- my_df %>%

      filter(
        Cruise == cruise.list[i],
        Depth_interval == interval.list[i])

    df.nmds <- metaMDS(df, trymax = 1000, k = 3, distance = "bray")

    # Plot 
    ordiplot(df.nmds, disp="sites", type="p", cex.lab=1.3)
    title(main=paste(cruise.list[i], "\n", interval.list[i],"\n", "stress : ", round(df.nmds$stress,2),sep=""), cex.main=1.5)

    dev.off()

  }

}

With this code, I only get 3 plots rather than the 15 expected. 使用此代码,我只能得到3个图,而不是预期的15个图。 I guess that the for loops are order in a way that R does not read the conditions as I expect. 我猜想for循环是有序的,R不会像我期望的那样读取条件。

Thanks for any help, 谢谢你的帮助,

Bests 贝斯茨

You have used the same "looping variable" in both loops. 您在两个循环中都使用了相同的“循环变量”。 For loops work by assigning the current value of the sequence to the variable you specify, so in your nested loops, first the current cruise gets assigned to i , and then immediately after, i gets overwritten with the current interval, and you have no way of accessing the current cruise anymore, eg: 对于循环,通过将序列的当前值分配给您指定的变量来工作,因此在嵌套循环中,首先将当前巡航分配给i ,然后紧接着, i被当前间隔所覆盖,您无法访问当前巡航的信息,例如:

cruise_list = c("Princess", "White Star")
interval_list = c("Summer", "winter")

for (i in cruise_list) {
    print(paste("Outer loop: i =", i))
    for (i in interval_list) {
        print(paste("Inner loop: i =", i))
    }
}
[1] "Outer loop: i = Princess"
[1] "Inner loop: i = Summer"
[1] "Inner loop: i = winter"
[1] "Outer loop: i = White Star"
[1] "Inner loop: i = Summer"
[1] "Inner loop: i = winter"

Also note that you can iterate directly over vectors like cruise_list as I've done above without going through seq_along and generating indexes, doing it directly like that might have helped you notice the problem sooner. 还要注意,您可以像cruise_list那样直接在类似cruise_list向量上进行迭代,而无需通过seq_along并生成索引,这样直接进行操作可能有助于您早日发现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM