1. ## par(mfrow=c(n1,n2))

Dear R users,

I want to add many graphs in a same window which can be done using par(mfrow=c(n1,n2) option. However, it won't be a good idea to have a crowded window.
So, I was wondering how to control the par(mfrow=c(n1,n2)) option. What do I mean by that? Here's an example.

Code:
``````# toy data
test<-data.frame(a=rep(seq(1,100,1),10), b=rnorm(100),c=rnorm(100))
test<-test[order(test\$a),]
test\$a<-as.factor(test\$a)  # specify a as a factor

# What I want to do is plot b V c for each level of factor a.
# See the levels of a
>length(unique(test\$a))
[1] 100              # so there are 100 levels of a i.e. 100 plots.

# Plot 'em now using my fave new stuff: Cut and List apply
cuts<-split(test, test\$a)

par(mfrow=c(n1,n2))     # some n1 and n2
my.plot<-lapply(cuts, function (test) {
plot(test\$b,test\$c, main=paste(test[1,1]))
})``````
As we can imagine, having 100 plots in a same window won't be possible. So, I was thinking to have, say 10 windows, and each plot accommodates 10 factors.

So, we have to specify n1 and n2 carefully.

Code:
``par(mfrow=c(2,5))     # for 10 plots``
So, how can I have a new window (dev.new()) after plotting every 10 levels of a.
So, how can we have 10 different windows, each having plots for 10 levels of a?

Better still: How can we smartly specify n1 and n2 so that we won't get this error on margin.

Code:
``Error in plot.new() : figure margins too large``
Many Thanks.

2. ## Re: par(mfrow=c(n1,n2))

Originally Posted by ledzep
So, how can I have a new window (dev.new()) after plotting every 10 levels of a.
So, how can we have 10 different windows, each having plots for 10 levels of a?

Originally Posted by ledzep
Better still: How can we smartly specify n1 and n2 so that we won't get this error on margin.
Other than trial and error, I'm not sure

3. ## The Following User Says Thank You to Jake For This Useful Post:

ledzep (02-19-2012)

4. ## Re: par(mfrow=c(n1,n2))

You could also create a new plotting device.

Code:
``````# If you're using windows I think this is the best
windows()
# On Linux
x11()
# On a mac
quartz() # I think?``````

5. ## Re: par(mfrow=c(n1,n2))

Thanks for the suggestion Jake.
Code:
``````# approach 1
par(mfrow=c(2,5))
my.plot<-lapply(cuts, function (test) {
plot(test\$b,test\$c, main=paste(test[1,1]))
})

# Approach 2
par(mfrow=c(2,5))
my.plot<-lapply(cuts, function (test) {
plot(test\$b,test\$c, main=paste(test[1,1]))
})

# What I get back

Waiting to confirm page change...
Waiting to confirm page change...
Waiting to confirm page change...
Waiting to confirm page change...
Waiting to confirm page change...
Waiting to confirm page change...
Waiting to confirm page change...

# then I have to click on the window and new plots get plotted every time I click. BUT the active window gets re-used. The previous plots are not stored as inactive windows at all. Just the active windows gets refreshed all the time.``````

6. ## Re: par(mfrow=c(n1,n2))

Give my way a try - it creates a separate active window so the old plots are saved on an inactive window.

7. ## Re: par(mfrow=c(n1,n2))

Funny, copying/pasting the code for Approach 1 and Approach 2 above both work identically (and correctly) for me. I don't see anything about waiting to confirm a page change, I just use the enter/return key to step through each plot. Maybe this is something to do with using base RGui vs. Rstudio. I am using Rstudio and TBH this isn't really a problem in the first place--the code from the OP creates ten separate plots and stores them all separately in the plot window.

8. ## Re: par(mfrow=c(n1,n2))

Problem seems to be where I keep windows() option.

Code:
``````# Appraoch 1 (Result: the same window gets refreshed all the time; no inactive windows)
windows()
par(mfrow=c(2,5))
my.plot<-lapply(cuts, function (test) {
plot(test\$b,test\$c, main=paste(test[1,1]))
})

# Appraoch 2 (throws error message)
par(mfrow=c(2,5))     # some n1 and n2
my.plot<-lapply(cuts, function (test) {
windows()
plot(test\$b,test\$c, main=paste(test[1,1]))
})

Error in windows() : too many open devices``````

9. ## Re: par(mfrow=c(n1,n2))

Originally Posted by Jake
Funny, copying/pasting the code for Approach 1 and Approach 2 above both work identically (and correctly) for me. I don't see anything about waiting to confirm a page change, I just use the enter/return key to step through each plot. Maybe this is something to do with using base RGui vs. Rstudio. I am using Rstudio and TBH this isn't really a problem in the first place--the code from the OP creates ten separate plots and stores them all separately in the plot window.
This is spooky. Same code, works for you but not for me!

Am I missing something, some plugins?

10. ## Re: par(mfrow=c(n1,n2))

Oh you're trying to mix windows and the par approach.

Everytime windows() gets called it creates a new plotting device. You'd only want to call windows after you filled the current window.

11. ## Re: par(mfrow=c(n1,n2))

Originally Posted by ledzep
This is spooky. Same code, works for you but not for me!

Am I missing something, some plugins?
Not sure. You are using Rstudio as well? What version of R are you running? I am on 2.14.1.

12. ## Re: par(mfrow=c(n1,n2))

I have got R.2.130.0 (released 2011-04-13).

13. ## Re: par(mfrow=c(n1,n2))

Something like this works for me:

Code:
``````datasets <- list()

for(i in 1:50){
datasets[[i]] <- rnorm(20)

id <- 1
for(i in 1:5){
windows()
par(mfrow = c(2,5))
for(j in 1:10){
hist(datasets[[id]])
id <- id + 1
}
}``````

14. ## The Following User Says Thank You to Dason For This Useful Post:

ledzep (02-19-2012)