0

I have the following code,

for (i in 1:length(split_fill_data)) {
  new_frame <- split_fill_data[i]
  new_frame_2 <- do.call(rbind.data.frame, new_frame)
  if(is.element(head(new_frame_2["egress"],1), unlist(mkt_out_60["egress"]))) 
      {
        print(head(arrange(new_frame_2,desc(Bytes_Outside))),5)

        #print('hello')
        plot(new_frame_2$ingress, new_frame_2$Bytes_Outside, main=head(new_frame_2["egress"],1))
        #x11()
        }
  }

The if block is true about 30 times and I want plot() to print a graph of ingress vs. Bytes_Outside for each of those 30 times. So, multiple subplots on a single window (or plot?).

How do I make this happen in RStudio?

M--
  • 25,431
  • 8
  • 61
  • 93
SampyKIshan
  • 29
  • 1
  • 7
  • 1
    `par(mfrow = c(5, 6))`? Or any other way of doing multiple plots... – Gregor Thomas Nov 19 '18 at 21:32
  • @Gregor problem with using `par` is that you have to know the number of plots (not exactly, but in a sense) beforehand. – M-- Nov 19 '18 at 21:58
  • https://stackoverflow.com/questions/10706753/how-do-i-arrange-a-variable-list-of-plots-using-grid-arrange – M-- Nov 19 '18 at 22:11
  • @Masoud "*The if block is true about 30 times*", sounds like OP has a sense of how many plots. Make it `mfrow = c(6, 6)` for a little cushion. Otherwise use `ggplot`, save the plots to a list, and then stick them together at the end (which is, I suppose, what your link is suggesting). – Gregor Thomas Nov 19 '18 at 22:12
  • 1
    @Gregor I didn't mean your solution won't help the OP. I was talking about it in a broader sense. Imagine you have a for-loop and if statement. As you change the parameters the ballpark number of plots would change so using `par` would not be very sufficient. Cheers – M-- Nov 19 '18 at 22:16

1 Answers1

0

A clunky fix would be to use par(mfrow = c(x,x) by running the for loop once and recording the number of true cases (say in y). Then in a second successive for loop defining the mfrow argument based on the results of the first for loop, where x would equal the square root of the length of y rounded up (i.e. ceiling(sqrt(length(y))). It's hard to write an example (at least for me) without some data to try it out on though. If you post some I will give it a crack. It's not the most elegant solution but I think it might get the job done!

André.B
  • 617
  • 8
  • 17