How to print multiple plots for each for loop iteration in R?












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?










share|improve this question




















  • 1





    par(mfrow = c(5, 6))? Or any other way of doing multiple plots...

    – Gregor
    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-M
    Nov 19 '18 at 21:58











  • stackoverflow.com/questions/10706753/…

    – M-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
    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-M
    Nov 19 '18 at 22:16


















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?










share|improve this question




















  • 1





    par(mfrow = c(5, 6))? Or any other way of doing multiple plots...

    – Gregor
    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-M
    Nov 19 '18 at 21:58











  • stackoverflow.com/questions/10706753/…

    – M-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
    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-M
    Nov 19 '18 at 22:16
















0












0








0


1






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?










share|improve this question
















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?







r plot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 21:29









M-M

6,70661944




6,70661944










asked Nov 19 '18 at 21:03









SampyKIshanSampyKIshan

24




24








  • 1





    par(mfrow = c(5, 6))? Or any other way of doing multiple plots...

    – Gregor
    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-M
    Nov 19 '18 at 21:58











  • stackoverflow.com/questions/10706753/…

    – M-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
    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-M
    Nov 19 '18 at 22:16
















  • 1





    par(mfrow = c(5, 6))? Or any other way of doing multiple plots...

    – Gregor
    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-M
    Nov 19 '18 at 21:58











  • stackoverflow.com/questions/10706753/…

    – M-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
    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-M
    Nov 19 '18 at 22:16










1




1





par(mfrow = c(5, 6))? Or any other way of doing multiple plots...

– Gregor
Nov 19 '18 at 21:32





par(mfrow = c(5, 6))? Or any other way of doing multiple plots...

– Gregor
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-M
Nov 19 '18 at 21:58





@Gregor problem with using par is that you have to know the number of plots (not exactly, but in a sense) beforehand.

– M-M
Nov 19 '18 at 21:58













stackoverflow.com/questions/10706753/…

– M-M
Nov 19 '18 at 22:11





stackoverflow.com/questions/10706753/…

– M-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
Nov 19 '18 at 22:12







@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
Nov 19 '18 at 22:12






1




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-M
Nov 19 '18 at 22:16







@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-M
Nov 19 '18 at 22:16














1 Answer
1






active

oldest

votes


















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!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382587%2fhow-to-print-multiple-plots-for-each-for-loop-iteration-in-r%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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!






    share|improve this answer




























      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!






      share|improve this answer


























        0












        0








        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!






        share|improve this answer













        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 22:13









        André.BAndré.B

        1519




        1519






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382587%2fhow-to-print-multiple-plots-for-each-for-loop-iteration-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents