+ Reply to Thread
Results 1 to 13 of 13

Thread: par(mfrow=c(n1,n2))

  1. #1
    Point Mass at Zero
    Points: 5,855, Level: 49
    Level completed: 53%, Points required for next Level: 95
    ledzep's Avatar
    Location
    Berks,UK
    Posts
    635
    Thanks
    169
    Thanked 130 Times in 128 Posts

    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.
    Oh Thou Perelman! Poincare's was for you and Riemann's is for me.

  2. #2
    Cookie Scientist
    Points: 5,895, Level: 49
    Level completed: 73%, Points required for next Level: 55
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    792
    Thanks
    17
    Thanked 313 Times in 239 Posts

    Re: par(mfrow=c(n1,n2))

    Quote Originally Posted by ledzep View Post
    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?
    Turn on devAskNewPage(T) before you draw your plots.

    Quote Originally Posted by ledzep View Post
    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
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

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

    ledzep (02-19-2012)

  4. #3
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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?
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  5. #4
    Point Mass at Zero
    Points: 5,855, Level: 49
    Level completed: 53%, Points required for next Level: 95
    ledzep's Avatar
    Location
    Berks,UK
    Posts
    635
    Thanks
    169
    Thanked 130 Times in 128 Posts

    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) {
    	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.

  6. #5
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  7. #6
    Cookie Scientist
    Points: 5,895, Level: 49
    Level completed: 73%, Points required for next Level: 55
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    792
    Thanks
    17
    Thanked 313 Times in 239 Posts

    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.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  8. #7
    Point Mass at Zero
    Points: 5,855, Level: 49
    Level completed: 53%, Points required for next Level: 95
    ledzep's Avatar
    Location
    Berks,UK
    Posts
    635
    Thanks
    169
    Thanked 130 Times in 128 Posts

    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
    Oh Thou Perelman! Poincare's was for you and Riemann's is for me.

  9. #8
    Point Mass at Zero
    Points: 5,855, Level: 49
    Level completed: 53%, Points required for next Level: 95
    ledzep's Avatar
    Location
    Berks,UK
    Posts
    635
    Thanks
    169
    Thanked 130 Times in 128 Posts

    Re: par(mfrow=c(n1,n2))

    Quote Originally Posted by Jake View Post
    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?
    Oh Thou Perelman! Poincare's was for you and Riemann's is for me.

  10. #9
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  11. #10
    Cookie Scientist
    Points: 5,895, Level: 49
    Level completed: 73%, Points required for next Level: 55
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    792
    Thanks
    17
    Thanked 313 Times in 239 Posts

    Re: par(mfrow=c(n1,n2))

    Quote Originally Posted by ledzep View Post
    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.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  12. #11
    Point Mass at Zero
    Points: 5,855, Level: 49
    Level completed: 53%, Points required for next Level: 95
    ledzep's Avatar
    Location
    Berks,UK
    Posts
    635
    Thanks
    169
    Thanked 130 Times in 128 Posts

    Re: par(mfrow=c(n1,n2))

    I have got R.2.130.0 (released 2011-04-13).
    I shall immediately upgrade mine!

  13. #12
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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
      }
    }
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

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

    ledzep (02-19-2012)

  15. #13
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: par(mfrow=c(n1,n2))


    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.

  16. The Following User Says Thank You to bryangoodrich For This Useful Post:

    ledzep (02-21-2012)

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats