ePoQ
ePoQ

Reputation: 463

Record a series of plot using a loop

I do have a vector called primes of length 100 and containing the first one hundred primes number as follow :

primes
  [1]   2   3   5   7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97 101 103 107 109
 [30] 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271
 [59] 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449
 [88] 457 461 463 467 479 487 491 499 503 509 521 523 541

I also have a dataframe called optm with all those primes as variables and a decreasing learning rate as rows. Here is for example what I obtain by plotting the 37th primes :

plot.default(optm$step, optm$seed37, type = "l", ylab = "learning rate",
             xlab = "first third iterations", main = 'seed37')

enter image description here

I would like to extract and save all these one hundred plot on a 4*5 multiple frame. As a result I would like 5 files (= 100 plots), that's why I implemented a as a recording limit within the following loop :

   a <- 20
    par(mfrow = c(4, 5))
    for(i in primes) { 
      cln <- paste0("seed",i)
      plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)
      for(j in 1:length(primes)) { 
      if (j==a){
             dev.copy(png, paste0(j,'optm.png'),width=8,height=6,units="in",res=100)
             a = a + 20
             dev.off()                 
    }
    }
    }

Problem is the recorded files on my computer are similar and possess the very same plot (the first one seed2). Where is my mistake ?

EDIT:

a = 0
par(mfrow = c(4, 5))
for(i in primes) { for(j in length(primes)) {cln <- paste0("seed",i)
               plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)  
               a = (a +1)
               if(a==20) {
                dev.copy(png, paste0(j,'optm.png'),width=8,height=6,units="in",res=100)
                dev.off()
                                                          }
     }
}

This pattern works for the first 20. So I coded a seq like this :

foo = seq(0, length(primes), by=20)

Now, I need to call any value of foo instead of 20 in my if command. How can I manage that ?

Upvotes: 0

Views: 275

Answers (2)

ePoQ
ePoQ

Reputation: 463

Sorry for answering my own question but I did find a way. Not the best one assurely but the following code is working.

a = 0
b = 20
par(mfrow = c(4, 5))
for(i in primes) {cln <- paste0("seed",i)
               plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)  
               a = (a +1)
               print(a)
               if (a == b) {
               dev.copy(png, paste0(a,'optm.png'),width=8,height=6,units="in",res=100)
               b =(b +20)
               dev.off()                                                        }
     }

Each time a reachs b a record of the plot series is made. Each time a reachs b, b increases by 20. I am open to any method to optimize it, especially by removing a and b and calling a kind of seq like this one:

foo = seq(0, length(primes), by=20)

Upvotes: 0

Jthorpe
Jthorpe

Reputation: 10203

you only need 1 for loop:

a <- 20
par(mfrow = c(4, 5))
for(i in seq(length(primes))) { 
  cln <- paste0("seed",primes[i])
  plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)
  if (i&&a == 0){
         dev.copy(png, paste0(i,'optm.png'),width=8,height=6,units="in",res=100)
         dev.off()                 
  }
}

Upvotes: 1

Related Questions