Modify ggdist interval thickness?

Viewed 33

I’m visualizing some distributions with the ggdist package and would like to modify the width of the interval lines. For example, a basic plot created with stat_histinterval() creates a histogram with an interval at the bottom.

library(tidyverse)
library(ggdist)

set.seed(123)

dist <-
  tibble(p_grid = seq(from = 0, to = 1, length.out = 1000),
         prior = rep(1, times = 1000)) %>%
  mutate(likelihood = dbinom(4, size = 15, prob = p_grid),
         posterior = likelihood * prior,
         posterior = posterior / sum(posterior)) %>% 
  slice_sample(n = 10000, weight_by = posterior, replace = TRUE)

ggplot(dist, aes(x = p_grid)) +
  stat_histinterval(.width = c(0.67, 0.89))

What I would like to do is make the black interval lines thicker. From the documentation, it seems like the interval_size argument is what I need. However, specifying an interval size overwrites the entire interval (i.e., it looks like one interval instead of a 67% and 89% interval).

ggplot(dist, aes(x = p_grid)) +
  stat_histinterval(interval_size = 5)

And specifying multiple sizes to the interval_size argument errors out.

ggplot(dist, aes(x = p_grid)) +
  stat_histinterval(interval_size = c(5, 10))
#> Error: Aesthetics must be either length 1 or the same as the data (34): interval_size

Is there a way to modify the interval's thickness while preserving the presence of multiple intervals?

Created on 2022-01-14 by the reprex package (v2.0.1)

2 Answers

The argument for this is interval_size_range which for some reason is only documented on geom_slabinterval despite working in other functions:

ggplot(dist, aes(x = p_grid)) +
    stat_histinterval(.width = c(0.67, 0.89),
                      interval_size_range = c(1, 3))

Plot with thicker interval

To eliminate the giant point, you want to change the default value of fatten_point which expands that point. For some reason, fatten_point also affects the size of the interval, so you'll need to increase the interval_size_range to compensate with a matching line size:

ggplot(dist, aes(x = p_grid)) +
    stat_histinterval(.width = c(0.67, 0.89),
                      interval_size_range = c(2, 5),
                      fatten_point = 1)

Plot with thicker interval without giant point

Could you just use two interval statements?

ggplot(dist, aes(x = p_grid)) +
  stat_histinterval(.width = .89, interval_size=10) + 
  stat_interval(.width = .67, interval_size=10, col="black", show.legend=FALSE) 

enter image description here

The problem is that there is a bit of overplotting. You could just add all of the individual elements:

ggplot(dist, aes(x = p_grid)) +
  geom_histogram(fill="gray55") + 
  stat_interval(.width = .67, interval_size=10, col="black", show.legend = FALSE) + 
  stat_interval(.width = .89, interval_size=5,col="black", show.legend = FALSE) + 
  geom_point(data=dist, aes(x=mean(p_grid), y=0), col="white", inherit.aes = FALSE, size=5) 

enter image description here

Related