How to plot a series of points on the same graph in for loop
I am relatively new to programming. So please bear with me.
plot(i, ex, xlim=c(0,l), ylim=c(0,15), type="o",
xlab="Current position", ylab="Current State of charge"
This is the code I formulated for the plot inside a for loop. But the above code produces an animation of the points on the plot and not a continuous segment (i.e.) the previous points on the plot are erased after each iteration.
Can someone please help me form a continuous series of points on a single plot.
Thank you in advance.
r for-loop plot
add a comment |
I am relatively new to programming. So please bear with me.
plot(i, ex, xlim=c(0,l), ylim=c(0,15), type="o",
xlab="Current position", ylab="Current State of charge"
This is the code I formulated for the plot inside a for loop. But the above code produces an animation of the points on the plot and not a continuous segment (i.e.) the previous points on the plot are erased after each iteration.
Can someone please help me form a continuous series of points on a single plot.
Thank you in advance.
r for-loop plot
You can addggplot
s using+
– 12b345b6b78
Nov 25 '18 at 22:50
add a comment |
I am relatively new to programming. So please bear with me.
plot(i, ex, xlim=c(0,l), ylim=c(0,15), type="o",
xlab="Current position", ylab="Current State of charge"
This is the code I formulated for the plot inside a for loop. But the above code produces an animation of the points on the plot and not a continuous segment (i.e.) the previous points on the plot are erased after each iteration.
Can someone please help me form a continuous series of points on a single plot.
Thank you in advance.
r for-loop plot
I am relatively new to programming. So please bear with me.
plot(i, ex, xlim=c(0,l), ylim=c(0,15), type="o",
xlab="Current position", ylab="Current State of charge"
This is the code I formulated for the plot inside a for loop. But the above code produces an animation of the points on the plot and not a continuous segment (i.e.) the previous points on the plot are erased after each iteration.
Can someone please help me form a continuous series of points on a single plot.
Thank you in advance.
r for-loop plot
r for-loop plot
asked Nov 25 '18 at 22:43
Ajith ViswanathAjith Viswanath
32
32
You can addggplot
s using+
– 12b345b6b78
Nov 25 '18 at 22:50
add a comment |
You can addggplot
s using+
– 12b345b6b78
Nov 25 '18 at 22:50
You can add
ggplot
s using +
– 12b345b6b78
Nov 25 '18 at 22:50
You can add
ggplot
s using +
– 12b345b6b78
Nov 25 '18 at 22:50
add a comment |
1 Answer
1
active
oldest
votes
You points are "erased" because you are creating a new plot every time you call the plot
command. One way around this is to create and empty plot with plot
and then add points with the points
command inside the loop:
# empty plot
plot(x=NA, y=NA, xlim=c(1,10), ylim=c(1,10), xlab="", ylab="", main="")
# add points
for (i in 1:10) {
points(x=rep(i,i), y=1:i, pch=20)
}
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
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%2f53472742%2fhow-to-plot-a-series-of-points-on-the-same-graph-in-for-loop%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
You points are "erased" because you are creating a new plot every time you call the plot
command. One way around this is to create and empty plot with plot
and then add points with the points
command inside the loop:
# empty plot
plot(x=NA, y=NA, xlim=c(1,10), ylim=c(1,10), xlab="", ylab="", main="")
# add points
for (i in 1:10) {
points(x=rep(i,i), y=1:i, pch=20)
}
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
add a comment |
You points are "erased" because you are creating a new plot every time you call the plot
command. One way around this is to create and empty plot with plot
and then add points with the points
command inside the loop:
# empty plot
plot(x=NA, y=NA, xlim=c(1,10), ylim=c(1,10), xlab="", ylab="", main="")
# add points
for (i in 1:10) {
points(x=rep(i,i), y=1:i, pch=20)
}
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
add a comment |
You points are "erased" because you are creating a new plot every time you call the plot
command. One way around this is to create and empty plot with plot
and then add points with the points
command inside the loop:
# empty plot
plot(x=NA, y=NA, xlim=c(1,10), ylim=c(1,10), xlab="", ylab="", main="")
# add points
for (i in 1:10) {
points(x=rep(i,i), y=1:i, pch=20)
}
You points are "erased" because you are creating a new plot every time you call the plot
command. One way around this is to create and empty plot with plot
and then add points with the points
command inside the loop:
# empty plot
plot(x=NA, y=NA, xlim=c(1,10), ylim=c(1,10), xlab="", ylab="", main="")
# add points
for (i in 1:10) {
points(x=rep(i,i), y=1:i, pch=20)
}
answered Nov 25 '18 at 22:59
Dan YDan Y
4,0081628
4,0081628
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
add a comment |
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Thank you for the help. Although it didn't give the direct solution, it helped me find the solution for my code.
– Ajith Viswanath
Nov 26 '18 at 0:32
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Hard to give you exact code for your situation when you didn't provide any example data or much context, but I'm glad it steered you in the right direction.
– Dan Y
Nov 26 '18 at 0:55
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Sorry for that. But it was really helpful. Grateful for that.
– Ajith Viswanath
Nov 26 '18 at 2:27
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Same - was on my phone and so a bit curt. Here's my usual response that I regularly copy/paste on SO: Please provide an MCVE (a minimum, complete, and verifiable example). Good advice for R-specific MVCEs is available here and here. All the best to you, -Dan.
– Dan Y
Nov 26 '18 at 2:30
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
Thank you for the comment. Will keep this in mind next time.
– Ajith Viswanath
Nov 26 '18 at 22:12
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%2f53472742%2fhow-to-plot-a-series-of-points-on-the-same-graph-in-for-loop%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
You can add
ggplot
s using+
– 12b345b6b78
Nov 25 '18 at 22:50