Last active
October 26, 2020 22:47
-
-
Save yjunechoe/bc4050a8f70a2a4eae01048d886b221b to your computer and use it in GitHub Desktop.
Tracking the mean through 100 samples form bivariate uniform distribution ~ unif(0, 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(slider) | |
library(gganimate) | |
points <- tibble( | |
id = 1:100, | |
x = runif(100), | |
y = runif(100), | |
time = 1:100, | |
size = 2, | |
color = "black" | |
) | |
means <- tibble( | |
id = 101, | |
x = slide_dbl(points$x, mean, .before = Inf)[-1], | |
y = slide_dbl(points$y, mean, .before = Inf)[-1], | |
time = 2:100, | |
size = 4, | |
color = "red" | |
) | |
p <- bind_rows( | |
points, | |
means | |
) %>% | |
ggplot(aes(x, y, group = id)) + | |
geom_hline(aes(yintercept = 0.5), linetype = 2) + | |
geom_vline(aes(xintercept = 0.5), linetype = 2) + | |
geom_segment( | |
aes(x = x1, y = y1, xend = next_x, yend = next_y), | |
color = "red", | |
alpha = .3, | |
data = means %>% | |
mutate( | |
next_x = lead(x), | |
next_y = lead(y) | |
) %>% | |
rename(x1 = x, y1 = y) %>% | |
mutate(id = row_number()+200) | |
) + | |
geom_rug( | |
aes( | |
color = ifelse(id == 101, "red", "black"), | |
) | |
) + | |
scale_size_identity() + | |
scale_color_identity() + | |
geom_point(aes(size = size, color = color)) + | |
theme_void() + | |
theme(plot.margin = margin(1, 1, 1, 1, "cm")) + | |
transition_reveal(time) | |
animate(p, res = 300, width = 1200, height = 1200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment