Cathy
Cathy

Reputation: 67

plotting using a for loop

I want to plot a number of graph using this for loop. however, I can only get one output (foo0001).

for (i in 1:5) {
 bitmap("foo%03d.jpg")
 plot(runif(20), ylim = c(0, 1))
 dev.off()
}

please help!

Upvotes: 0

Views: 151

Answers (1)

nneonneo
nneonneo

Reputation: 179392

bitmap writes each page (plot) to consecutive files according to the format string chosen. Calling bitmap creates a new graphics device, resetting the page number. So, by plotting one plot per bitmap call, you are always writing to foo0001.jpg.

Instead, call bitmap just once:

bitmap("foo%03d.jpg")
for (i in 1:5) {
     plot(runif(20), ylim = c(0, 1))
}
dev.off()

Upvotes: 2

Related Questions