How to print multiple plots for each for loop iteration in R?
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 21:32
@Gregor problem with usingpar
is that you have to know the number of plots (not exactly, but in a sense) beforehand.
– Masoud
Nov 19 at 21:58
stackoverflow.com/questions/10706753/…
– Masoud
Nov 19 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 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
– Masoud
Nov 19 at 22:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 21:29
Masoud
6,48241442
6,48241442
asked Nov 19 at 21:03
SampyKIshan
24
24
1
par(mfrow = c(5, 6))
? Or any other way of doing multiple plots...
– Gregor
Nov 19 at 21:32
@Gregor problem with usingpar
is that you have to know the number of plots (not exactly, but in a sense) beforehand.
– Masoud
Nov 19 at 21:58
stackoverflow.com/questions/10706753/…
– Masoud
Nov 19 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 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
– Masoud
Nov 19 at 22:16
add a comment |
1
par(mfrow = c(5, 6))
? Or any other way of doing multiple plots...
– Gregor
Nov 19 at 21:32
@Gregor problem with usingpar
is that you have to know the number of plots (not exactly, but in a sense) beforehand.
– Masoud
Nov 19 at 21:58
stackoverflow.com/questions/10706753/…
– Masoud
Nov 19 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 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
– Masoud
Nov 19 at 22:16
1
1
par(mfrow = c(5, 6))
? Or any other way of doing multiple plots...– Gregor
Nov 19 at 21:32
par(mfrow = c(5, 6))
? Or any other way of doing multiple plots...– Gregor
Nov 19 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.– Masoud
Nov 19 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.– Masoud
Nov 19 at 21:58
stackoverflow.com/questions/10706753/…
– Masoud
Nov 19 at 22:11
stackoverflow.com/questions/10706753/…
– Masoud
Nov 19 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 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 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– Masoud
Nov 19 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– Masoud
Nov 19 at 22:16
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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 |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 22:13
André.B
257
257
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 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.– Masoud
Nov 19 at 21:58
stackoverflow.com/questions/10706753/…
– Masoud
Nov 19 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 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– Masoud
Nov 19 at 22:16