Free flowing look on raincloud plot

102 views Asked by At

I am creating several raincloud plots and am wondering how I can change the style to look more free flowing like [![this]

here is the code I am using adapted to iris.

    iris %>%
      dplyr::group_by(Species) %>%
      dplyr::mutate(
        mean = mean(Petal.Length),
        se = sd(Petal.Length) / sqrt(length(Petal.Length)),
        species_y = paste0(Species, "\n(", n(), ")")
      ) %>%
      ungroup() %>%
      ggplot(aes(x = Petal.Length, y = species_y)) +
      stat_slab(aes(fill = Species)) +
      stat_dots(aes(color = Species), side = "bottom", shape = 16) +
      scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
      geom_errorbar(aes(
        xmin = mean - 1.96 * se,
        xmax = mean + 1.96 * se
      ), width = 0.2) +
      stat_summary(fun = mean, geom = "point", shape = 16, size = 3.0) +
      theme_bw(base_size = 10)
1

There are 1 answers

2
Allan Cameron On

The "free-flowing" look can be achieved by using geom_point and ggpp::position_jitternudge

iris %>%
  dplyr::group_by(Species) %>%
  dplyr::mutate(
    mean = mean(Petal.Length),
    se = sd(Petal.Length) / sqrt(length(Petal.Length)),
    species_y = paste0(Species, "\n(", n(), ")")
  ) %>%
  ungroup() %>%
  ggplot(aes(x = Petal.Length, y = species_y)) +
  stat_slab(aes(fill = Species)) +
  geom_point(aes(color = Species),shape = 16,
             position = ggpp::position_jitternudge(height = 0.125, width = 0, 
                                             y = -0.125,
                                             nudge.from = "jittered")) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
  geom_errorbar(aes(
    xmin = mean - 1.96 * se,
    xmax = mean + 1.96 * se
  ), width = 0.2) +
  stat_summary(fun = mean, geom = "point", shape = 16, size = 3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top") +
  labs(title = "Raincloud plot with ggdist", x = "Petal Length")

enter image description here

The means are centered in the given example, so it's not really clear what you mean here.