-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom_df.R
65 lines (58 loc) · 1.83 KB
/
random_df.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Load packages
library(dplyr)
library(ggplot2)
library(ggdark)
# Random df based on two matrices
random_df <- function(x, y, n){
df <- data.frame(x = rep(x,n),y = rep(y,n), r = rep(1, n))
for (i in 2:n){
ctrl <- sample(c(0,1), 1, replace = TRUE)
if (ctrl == 0){ # Rotate previous point by pi/3
df$x[i] <- df$x[i-1]*cos(pi/3)-df$y[i-1]*sin(pi/3)
df$y[i] <- df$x[i-1]*sin(pi/3)+df$y[i-1]*cos(pi/3)
} else{ # Shift previous point right two down three
df$x[i] <- df$x[i-1]+2
df$y[i] <- df$y[i-1]-3
}
df$r[i] <- ctrl
}
df <- df %>%
mutate(r = as.factor(r))
return(df)
}
rdf <- random_df(1,1,1000)
ggplot(rdf, aes(x = x, y = y, color = r))+
geom_point()+
scale_color_viridis_d(option = "plasma")+
guides(color = "none")+
dark_theme_void()
--------------------------------------------------------------------------------
# Random rotation and random translation
random_df_2 <- function(x, y, n){
df <- data.frame(x = rep(x,n),y = rep(y,n), r = rep(1, n))
for (i in 2:n){
ctrl <- sample(c(0,1), 1)
rand1 <- sample(1:2, 1)
rand2 <- sample(2:10, 1)
rand3 <- sample(c(-1,1), 1)
rand4 <- sample(c(-1,1), 1)
if (ctrl == 0){ # Random rotation matrix
df$x[i] <- df$x[i-1]*cos(rand1*pi/rand2)-df$y[i-1]*sin(rand1*pi/rand2)
df$y[i] <- df$x[i-1]*sin(rand1*pi/rand2)+df$y[i-1]*cos(rand1*pi/rand2)
} else{ # Random translation
df$x[i] <- df$x[i-1]+rand3*rand1
df$y[i] <- df$y[i-1]+rand4*rand2
}
df$r[i] <- ctrl
}
df <- df %>%
mutate(r = as.factor(r))
return(df)
}
rdf_2 <- random_df_2(1,1,1000000)
rrdf <- random_df_2(runif(1), runif(1), sample(10000:100000,1))
ggplot(rrdf, aes(x = x, y = y, color = r))+
geom_point(alpha = 0.15)+
scale_color_manual(values = c("#23EDFF", "#F9FF23"))+
guides(color = "none")+
dark_theme_void()