Search code examples
rplotdirectiontimestep

Progressive point to point walk plotting in R


How can I to plot a progressive walk from point to point?

Lets have p1 =[1,0], p2=[0,1], p3=[1,1]. Plot should first draw a line from p1 to p2 showing the direction, wait for a second, then draw another line from p2 to p3 and it goes on if you have more data.

The plot size should be first fixed to (0,1)^2. Correct output should look similar to this image: Example plot

My code is this for now:

plot(x,y,xlim=range(x), ylim=range(y), xlab="x", ylab="y", main="Filled Plane",pch=16)
#lines(x,y,xlim=range(x),ylim=(y),pch=16)
for(i in 1:20){
  arrows(x[i],y[i],x[i+1],y[i+1],length = 0.25, angle = 30, col = 1:3)
}

Solution

  • One option is to use arrows. Fist you need to create a plot giving the data you want. Then you can draw lines to connect your points. Let say you have random uniform arrays of x,y. Set the limit to decide how many points you want to plot. Although I placed the points immediately ( I could not place the grid properly otherwise) Hope it helps.

    limit<- 50
    x <- runif(limit)
    y <- runif(limit)
    plot(x,y, xlim=range(0,1), ylim=range(0,1),
        xlab="x", ylab="y", main = "Random Walk")
    grid(nx = 10, ny = 10, col = "lightgray", lty = "dotted", 
        lwd = par("lwd"), equilogs = TRUE)
    for(i in 1:limit){
      arrows(x[i],y[i],x[i+1],y[i+1], length = 0.1, angle = 20)
      Sys.sleep(0.5)
    }