Skip to content

Instantly share code, notes, and snippets.

@mickaellegal
Created March 9, 2019 15:22
Show Gist options
  • Save mickaellegal/50d69f90749b3a81dc79410d4dc3202b to your computer and use it in GitHub Desktop.
Save mickaellegal/50d69f90749b3a81dc79410d4dc3202b to your computer and use it in GitHub Desktop.
Example of Simulation in R
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