'ggdist stat_halfeye not scaling correctly

I seem to have an error in the way my distribution looks. The bottom ridges of each of the facetted graphs are not at the same scale as the other ridges above, or relative to the number of counts (i.e. scale dots shown).

Is there a way to scale all distributions relative to one another?

season_names <- c(`0` = "COOL", `1` = "HOT-DRY",`2` = "HOT-WET")

dCLEAN %>%
  ggplot(aes(x = tdb, y = as.factor(tsv), fill = as.factor(season))) +
  ggdist::stat_halfeye(
    adjust = 0.9,
    justification = -0.15,
    .width = 0,
    point_colour = NA) +
  geom_boxplot(
    width = 0.2,
    outlier.colour = NA,
    alpha = 0.5)+
  ggdist::stat_dots(
    side = "left",
    justification = 1.18,
    binwidth = 0.1) +
  facet_wrap(~ season, labeller = as_labeller(season_names)) +
  theme_bw() +
  theme(strip.background = element_rect(fill="white")) +
  theme(legend.position = "none") +
  scale_color_grey()+
  scale_fill_grey()

Image of current graph (Errors seem to be in Cool graph -2 distribution, Hot-Dry graph -1 distribution, Hot-Wet graph -1 distribution



Solution 1:[1]

By default, the densities are scaled to have equal area regardless of the number of observations. If you wish to scale the areas according to the number of observations, you can set aes(thickness = stat(pdf*n)) in stat_halfeye(). This sets the thickness of the slab according to the product of two computed variables generated by stat_halfeye(): the density (pdf) and the number of observations per group (n).

Here's an example from the raincloud plots section of the dotsinterval vignette:

set.seed(12345) # for reproducibility

data.frame(
  abc = c("a", "b", "b", "c"),
  value = rnorm(200, c(1, 8, 8, 3), c(1, 1.5, 1.5, 1))
) %>%
  ggplot(aes(y = abc, x = value, fill = abc)) +
  stat_slab(aes(thickness = stat(pdf*n)), scale = 0.7) +
  stat_dotsinterval(side = "bottom", scale = 0.7, slab_size = NA) +
  scale_fill_brewer(palette = "Set2") 

Raincloud plot with three groups, showing the areas of the densities scaled according to the number of elements in each group

This example uses stat_slab() in place of stat_halfeye(). stat_slab() is a shortcut stat equivalent to stat_halfeye() but without the point and the interval (so it saves you passing .width = 0 and point_color = NA to stat_halfeye()).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthew Kay