Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Last active December 1, 2022 18:12
Show Gist options
  • Save andrewheiss/e4d5beea418fa41656d48c5dd45589a2 to your computer and use it in GitHub Desktop.
Save andrewheiss/e4d5beea418fa41656d48c5dd45589a2 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(ggdist)
library(patchwork)


# Single distributions ----------------------------------------------------

# Regular ggplot way
p1 <- ggplot() +
  stat_function(geom = "area", fun = ~dnorm(., mean = 2, sd = 1),
                fill = "grey65") +
  xlim(-1.3, 5.15) +
  labs(subtitle = "stat_function()")

# ggdist way
p2 <- ggplot() +
  stat_slab(aes(xdist = "norm", arg1 = 2, arg2 = 1)) +
  labs(subtitle = "ggdist and stat_slab()")

# ggdist way with parse_dist()
# This lets you use Stan-style distribution names, like student_t(3, 0, 2.5)
p3 <- parse_dist("normal(2, 1)") %>% 
  ggplot(aes(xdist = .dist, args = .args)) + 
  stat_slab() +
  labs(subtitle = "ggdist::parse_dist() and stat_slab()")
p1 / p2 / p3

# Multiple distributions --------------------------------------------------
# Regular ggplot way
p1 <- ggplot() +
  stat_function(geom = "area", fun = ~dnorm(., mean = 2, sd = 1), aes(fill = "A")) +
  stat_function(geom = "area", fun = ~dnorm(., mean = 10, sd = 2), aes(fill = "B")) +
  xlim(-1.3, 16.1) +
  labs(subtitle = "stat_function()")

# ggdist way
p2 <- tribble(
  ~name, ~mean, ~sd,
  "A",   2,     1,
  "B",   10,    2
) %>% 
  ggplot(aes(y = 0, xdist = "norm", arg1 = mean, arg2 = sd, fill = name)) +
  stat_slab() +
  labs(subtitle = "ggdist and stat_slab()")

# ggdist way with parse_dist()
p3 <- tribble(
  ~name, ~dist_string,
  "A",   "normal(2, 1)",
  "B",   "normal(10, 2)"
) %>% 
  mutate(dist = parse_dist(dist_string)) %>% 
  unnest(dist) %>% 
  ggplot(aes(y = 0, xdist = .dist, args = .args, fill = name)) +
  stat_slab() +
  labs(subtitle = "ggdist::parse_dist() and stat_slab()")
p1 / p2 / p3

# Other slab geoms --------------------------------------------------------
dists <- tribble(
  ~name, ~mean, ~sd,
  "A",   2,     1,
  "B",   10,    2
)

p1 <- ggplot(dists, aes(y = 0, xdist = "norm", arg1 = mean, arg2 = sd, fill = name)) +
  stat_halfeye() +
  labs(subtitle = "Half eye")

p2 <- ggplot(dists, aes(y = 0, xdist = "norm", arg1 = mean, arg2 = sd, fill = name)) +
  stat_dots() +
  labs(subtitle = "Dots")

p3 <- ggplot(dists, aes(y = 0, xdist = "norm", arg1 = mean, arg2 = sd, fill = name)) +
  stat_gradientinterval() +
  labs(subtitle = "Gradient interval")
p1 / p2 / p3
#> Warning: Using the `size` aesthietic with geom_segment was deprecated in ggplot2 3.4.0.
#> ℹ Please use the `linewidth` aesthetic instead.
#> Warning: fill_type = "gradient" is not supported by the current graphics device.
#>  - Falling back to fill_type = "segments".
#>  - If you believe your current graphics device *does* support
#>    fill_type = "gradient" but auto-detection failed, set that option
#>    explicitly and consider reporting a bug.
#>  - See help("geom_slabinterval") for more information.

# Using {distributional} --------------------------------------------------

library(distributional)

p1 <- ggplot() +
  stat_halfeye(aes(xdist = dist_normal(2, 1))) +
  labs(subtitle = "ggdist and dist_normal() and stat_halfeye()")

p2 <- tribble(
  ~name, ~dist, 
  "A",   dist_normal(2, 1),
  "B",   dist_normal(10, 2)
) %>% 
  ggplot(aes(y = 0, xdist = dist, fill = name)) +
  stat_halfeye() +
  labs(subtitle = "ggdist and dist_normal() and stat_halfeye()")
p1 / p2

Created on 2022-12-01 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment