Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Created January 1, 2016 21:50
Show Gist options
  • Save dmarcelinobr/7933706b5a07a32e84ce to your computer and use it in GitHub Desktop.
Save dmarcelinobr/7933706b5a07a32e84ce to your computer and use it in GitHub Desktop.
randomwalk
# animation-2.r
# author: Bryce Mecum ([email protected])
#
# Animations in R Tutorial
# Part 2 of 6
#
# Extend part 1 by plotting a random (uniform) walk starting in the bottom
# right-hand corner and moving North and West.
library(maps)
xrange <- 180:240
yrange <- 45:70
ndays <- 30
jump_distance_x <- 1
jump_distance_y <- 1
positions <- data.frame(day = 1:ndays, x = c(225, rep(NA, ndays - 1)), y = c(50, rep(NA, ndays - 1)))
positions
for(i in 2:ndays)
{
positions[i,"x"] <- positions[i - 1,"x"] + runif(1, -jump_distance_x, 0)
positions[i,"y"] <- positions[i - 1,"y"] + runif(1, 0, jump_distance_y)
}
positions
map('world2', xlim = range(xrange), ylim = range(yrange))
map.axes()
points(y ~ x,
data = positions,
cex = 1,
col = "red",
pch = 19)
lines(y ~ x, data = positions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment