Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Created February 25, 2019 23:56
Show Gist options
  • Select an option

  • Save dgrtwo/d590078aae86e24418a7cf5a9fb31ae3 to your computer and use it in GitHub Desktop.

Select an option

Save dgrtwo/d590078aae86e24418a7cf5a9fb31ae3 to your computer and use it in GitHub Desktop.

Revisions

  1. dgrtwo created this gist Feb 25, 2019.
    21 changes: 21 additions & 0 deletions dice-rolls.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Code behind this tweet: https://twitter.com/drob/status/1100182329350336513
    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")