Created
March 9, 2019 15:22
-
-
Save mickaellegal/50d69f90749b3a81dc79410d4dc3202b to your computer and use it in GitHub Desktop.
Example of Simulation in R
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(gganimate) | |
# Setup | |
options(gganimate.nframes = 200) | |
set.seed(2019) | |
simulation <- tibble(roll = 1:10000) %>% | |
mutate(result = sample(6, n(), replace = TRUE)) %>% | |
crossing(nrolls = seq(10, 10000, 10)) %>% | |
filter(roll <= nrolls) %>% | |
count(nrolls, result) | |
ggplot(simulation, aes(result, n, fill = factor(result))) + | |
geom_col(position = "identity", show.legend = FALSE) + | |
transition_manual(nrolls) + | |
view_follow() + | |
scale_x_continuous(breaks = 1:6) + | |
labs(y = "# of rolls with this result", | |
title = "Distribution of results after { current_frame } rolls of a six-sided die") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment