How to print multiple plots for each for loop iteration in R?
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
add a comment |
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
1
par(mfrow = c(5, 6))
? Or any other way of doing multiple plots...
– Gregor
Nov 19 '18 at 21:32
@Gregor problem with usingpar
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 itmfrow = c(6, 6)
for a little cushion. Otherwise useggplot
, 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 usingpar
would not be very sufficient. Cheers
– M-M
Nov 19 '18 at 22:16
add a comment |
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
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
r plot
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 usingpar
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 itmfrow = c(6, 6)
for a little cushion. Otherwise useggplot
, 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 usingpar
would not be very sufficient. Cheers
– M-M
Nov 19 '18 at 22:16
add a comment |
1
par(mfrow = c(5, 6))
? Or any other way of doing multiple plots...
– Gregor
Nov 19 '18 at 21:32
@Gregor problem with usingpar
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 itmfrow = c(6, 6)
for a little cushion. Otherwise useggplot
, 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 usingpar
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
add a comment |
1 Answer
1
active
oldest
votes
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!
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!
add a comment |
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!
add a comment |
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!
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!
answered Nov 19 '18 at 22:13
André.BAndré.B
1519
1519
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 useggplot
, 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