Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Dimensional Random Walks #40

Open
spsanderson opened this issue Jul 24, 2024 · 0 comments
Open

Two Dimensional Random Walks #40

spsanderson opened this issue Jul 24, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Jul 24, 2024

Some 2D random walks:

# Load required libraries
library(ggplot2)
library(dplyr)

# Function to generate a 2D random walk
generate_random_walk <- function(n_steps = 100) {
  # Generate random steps
  x_steps <- sample(c(-1, 1), n_steps, replace = TRUE)
  y_steps <- sample(c(-1, 1), n_steps, replace = TRUE)
  
  # Calculate positions
  x_positions <- cumsum(x_steps)
  y_positions <- cumsum(y_steps)
  
  # Create a data frame
  walk_df <- data.frame(
    Step = 1:n_steps,
    X = x_positions,
    Y = y_positions
  )
  
  return(walk_df)
}

# Generate a random walk
random_walk_df <- generate_random_walk(n_steps = 1000)

# Plot the random walk
ggplot(random_walk_df, aes(x = X, y = Y)) +
  geom_path(color = "blue", alpha = 0.7) +
  geom_point(color = "red", size = 1) +
  theme_minimal() +
  labs(
    title = "2D Random Walk",
    x = "X Position",
    y = "Y Position"
  ) +
  theme(axis.text.x=element_blank(), #remove x axis labels
        axis.ticks.x=element_blank(), #remove x axis ticks
        axis.text.y=element_blank(),  #remove y axis labels
        axis.ticks.y=element_blank()  #remove y axis ticks
  )

# Function to simulate a 2D random walk
random_walk <- function(steps = 1000, step_size = 1) {
  x <- cumsum(runif(steps, -step_size, step_size))
  y <- cumsum(runif(steps, -step_size, step_size))
  data.frame(step = 1:steps, x = x, y = y)
}

# Generate random walk data
walk_data <- random_walk(steps = 1000, step_size = 0.5)

# Plot the random walk
ggplot(walk_data, aes(x = x, y = y)) +
  geom_path(aes(color = step), linewidth = 0.5) +
  geom_point(data = walk_data[c(1, max(walk_data[["step"]])), ], 
             aes(color = step), size = 3) +
  scale_color_viridis_c(option = "turbo") +
  labs(title = "2D Random Walk",
       x = "X Position",
       y = "Y Position") +
  theme_minimal() +
  theme(legend.position = "none") +
  theme(axis.text.x=element_blank(), #remove x axis labels
        axis.ticks.x=element_blank(), #remove x axis ticks
        axis.text.y=element_blank(),  #remove y axis labels
        axis.ticks.y=element_blank()  #remove y axis ticks
  )

# Set random seed for reproducibility
set.seed(123)

# Number of steps
n <- 1000

# Generate random walk
random_walk <- data.frame(
  x = cumsum(rnorm(n)),
  y = cumsum(rnorm(n)),
  step = 1:n
)

# Create the plot
ggplot(random_walk, aes(x = x, y = y)) +
  geom_path(aes(color = step), linewidth = 0.5) +
  geom_point(data = random_walk[c(1, n), ], aes(color = step), size = 3) +
  scale_color_viridis_c(option = "plasma") +
  labs(title = "2D Random Walk",
       x = "X Position",
       y = "Y Position") +
  theme_minimal() +
  theme(legend.position = "none") +
  theme(axis.text.x=element_blank(), #remove x axis labels
        axis.ticks.x=element_blank(), #remove x axis ticks
        axis.text.y=element_blank(),  #remove y axis labels
        axis.ticks.y=element_blank()  #remove y axis ticks
  )

image

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

1 participant