rhombidodecahedron
rhombidodecahedron

Reputation: 7922

Is there any way to build two plots at the same time in R?

I want to make two 2x2 plots at the same time. My motivation is because I originally had logic like this:

pdf(...)
par(mfrow=c(2,2))
for (subplot_i in c(1,2,3,4)) {
    plot(f(subplot_i))
}
dev.off()

but now I want to do something like

pdf(..., window=1)
pdf(..., window=2)
par(mfrow=c(2,2))
for (subplot_i in c(1,2,3,4)) {
    plot(f(subplot_i), window=1)
    plot(g(subplot_i), window=2)
}
dev.off(window=1)
dev.off(window=2)

that is, build up two plots at the same time.

I could separate it into two loops, but this would double the processing that occurs inside of the loop. I could move all of this processing outside of the loop, but it would come at some considerable effort.

So is what I want possible?

Upvotes: 1

Views: 150

Answers (1)

rhombidodecahedron
rhombidodecahedron

Reputation: 7922

Appears to be possible!

f <- function(x) { x }
g <- function(x) { x^2 }
pdf('a.pdf')
par(mfrow=c(2,2))
pdf('b.pdf')
par(mfrow=c(2,2))
for (subplot_i in c(1,2,3,4)) {
    plot(f(subplot_i))
    dev.set(dev.prev())
    plot(g(subplot_i))
    dev.set(dev.next())
}
dev.off()
dev.off()

Upvotes: 1

Related Questions