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.
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.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])) })
So, we have to specify n1 and n2 carefully.
So, how can I have a new window (dev.new()) after plotting every 10 levels of a.Code:par(mfrow=c(2,5)) # for 10 plots
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.
Many Thanks.Code:Error in plot.new() : figure margins too large
Oh Thou Perelman! Poincare's was for you and Riemann's is for me.
ledzep (02-19-2012)
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?
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
Thanks for the suggestion Jake.
Code:# approach 1 par(mfrow=c(2,5)) my.plot<-lapply(cuts, function (test) { devAskNewPage(T) plot(test$b,test$c, main=paste(test[1,1])) }) # Approach 2 devAskNewPage(T) 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.
Oh Thou Perelman! Poincare's was for you and Riemann's is for me.
Give my way a try - it creates a separate active window so the old plots are saved on an inactive window.
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
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.
In God we trust. All others must bring data.
~W. Edwards Deming
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
Oh Thou Perelman! Poincare's was for you and Riemann's is for me.
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.
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
I have got R.2.130.0 (released 2011-04-13).
I shall immediately upgrade mine!
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 } }
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
ledzep (02-19-2012)
Don't feel like reading all the comments, but are you trying to plot for a final product or to view in a window? These are two different goals, and your reproduction method should guide your process. For instance, if you want to look at a bunch of plots, I usually just create an image book (pdf). I can just scroll each page and view each one (good if it's the same plotting space but changes in the variables, e.g., data at points in time). If you want to compare plots side-by-side, then you'll need to do some par(mfrow...) thing. However, if that is good ungodly to look at because you're trying to do some 6x12 grid or something, you should probably reconsider your goal! I find on typical reproductions that 2x2 or 3x3 grids work best, and it is better if you put them straight to an image where you can control the dimensions of your output. The plotting device is usually a 640x640 or something like that. You may prefer, instead, to set it to some widescreen resolution. You can adjust a lot of things in this case (e.g., font, resolution, titles, etc.) to get the best results. But as I said, this is all guided by what your reproduction method is going to be.
In your case, you say you'll have 100 plots. Are you actually comparing them side-by-side? Are they sequential? Are they independent? I would probably just do a standard plot to a pdf and give a title of which factor it is you're viewing or if you're comparing them in groups, a 2x10 with an appropriate resolution to a pdf could work, too. That's just my thoughts.
ledzep (02-21-2012)
|
|