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.

1 Answers1

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)
}
DanY
  • 5,920
  • 1
  • 13
  • 33
  • 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 00: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. – DanY Nov 26 '18 at 00:55
  • Sorry for that. But it was really helpful. Grateful for that. – Ajith Viswanath Nov 26 '18 at 02: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](https://stackoverflow.com/help/mcve) (a minimum, complete, and verifiable example).  Good advice for R-specific MVCEs is available [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and [here](https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html).  All the best to you, -Dan. – DanY Nov 26 '18 at 02:30
  • Thank you for the comment. Will keep this in mind next time. – Ajith Viswanath Nov 26 '18 at 22:12