How to plot a series of points on the same graph in for loop












0















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.










share|improve this question























  • You can add ggplots using +

    – 12b345b6b78
    Nov 25 '18 at 22:50


















0















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.










share|improve this question























  • You can add ggplots using +

    – 12b345b6b78
    Nov 25 '18 at 22:50
















0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 22:43









Ajith ViswanathAjith Viswanath

32




32













  • You can add ggplots using +

    – 12b345b6b78
    Nov 25 '18 at 22:50





















  • You can add ggplots using +

    – 12b345b6b78
    Nov 25 '18 at 22:50



















You can add ggplots using +

– 12b345b6b78
Nov 25 '18 at 22:50







You can add ggplots using +

– 12b345b6b78
Nov 25 '18 at 22:50














1 Answer
1






active

oldest

votes


















0














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)
}





share|improve this answer
























  • 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











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%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









0














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)
}





share|improve this answer
























  • 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
















0














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)
}





share|improve this answer
























  • 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














0












0








0







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)
}





share|improve this answer













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)
}






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















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%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





















































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

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen