Random Walk in ggplot2

How to make Random Walk in ggplot2 with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

A random walk can be thought of as a random process in which a token or a marker is randomly moved around some space, that is, a space with a metric used to compute distance. In the visualizations below, we will be using scatter plots as well as a colorscale to denote the time sequence of the walk.

Random Walk in 1D

The jitter in the data points along the x and y axes are meant to illuminate where the points are being drawn and what the tendency of the random walk is.

library(plotly)
library(ggplot2)

P1 <- cumsum(rnorm(1000))
dat <- data.frame(x = seq_along(P1), y = P1)
p <- ggplot(dat, aes(x = x, y = y)) + geom_line()

ggplotly(p)

Random Walk in 2D

library(plotly)
require(ggplot2)

random_walk <- function(n.org, steps, left.p = .5, up.p = .5, plot = TRUE){


whereto <- matrix(ncol = 2)

for(x in 1:n.org){
walker <- matrix(c(0,0), nrow = steps+1, ncol = 2, byrow = T)

for(i in 1:steps){
# left/right = 1/0
horizontal <- rbinom(1, 1, left.p)

# distance 2
h.dist <- abs(rnorm(1, 0, 1))

# Horizontal Movement
if(horizontal == 0){
walker[i+1,1] <- walker[i,1] + h.dist
}
if(horizontal == 1){
walker[i+1,1] <- walker[i,1] - h.dist
}

# up/down = 1/0
vertical <- rbinom(1, 1, up.p)

#distance 2
v.dist <- abs(rnorm(1, 0, 1))

# Vertical Movement
if(vertical == 1){
walker[i+1,2] <- walker[i,2] + v.dist
}
if(vertical == 0){
walker[i+1,2] <- walker[i,2] - v.dist
}
}

whereto <- rbind(whereto, walker)
}

id <- rep(1:n.org, each = 1001)
colnames(whereto) <- c("x" , "y")
whereto <- as.data.frame(whereto)
whereto <- cbind(whereto[2:nrow(whereto),], org = factor(id))

p <- ggplot(whereto, aes(x = x, y = y, colour = org))
p <- p + geom_path()

return(p)
}

p <- random_walk(1, 1000, .5, .5)

ggplotly(p)

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)