rplotconfidence-intervalviolin-plotggdist

Raincloud plot with confidence


I am trying to create a raincloud plot with a 95% confidence interval, however my code keeps on produces a raincloud plot with a boxplot interval. Meaning the black circle is the median not the mean. How can I change my code to add in error bars to achieve this?

Here are two codes I tried that create the raincloud plot, but no with the mean. I have attached how the plots look. The last image is how I want the plot to look. ")


Can be found here:
https://z3tt.github.io/Rainclouds/

ggplot(iris, aes(Species, Sepal.Width)) + ggdist::stat_halfeye(adjust = .5, width = .3, .width = c(0.5, 1)) + ggdist::stat_dots(side = "left", dotsize = .4, justification = 1.05, binwidth = .1)


Solution

  • Thanks for updating your question with a minimal reproducible example. One potential 'easy' solution is to add the mean to the existing plot:

    library(tidyverse)
    library(ggdist)
    
    iris %>%
      ggplot(aes(x=Petal.Length, y=Species))+
      stat_slab(aes(fill=Species), scale = 0.7) +
      stat_dotsinterval(aes(fill=Species),
                        side = "bottom",
                        scale = 0.7,
                        slab_size = NA) + 
      scale_fill_brewer(palette = "Set1") +
      stat_summary(aes(color = "mean"), 
                   fun=mean, geom="point", shape="|", size=5) +
      theme_bw(base_size = 16) +
      theme(legend.position = "top")+
    # scale_x_continuous(limits = c(-4.5, .5), breaks = seq(-4.5, 0.5, by = 0.5))+
      labs(title="Raincloud plot with ggdist") +
      scale_color_manual(name = "",
                         values = c("mean" = "cyan"))
    
    

    Created on 2023-03-02 with reprex v2.0.2

    If this doesn't suit, I suspect you need to calculate the statistics yourself, e.g.

    library(tidyverse)
    library(ggdist)
    iris %>%
      group_by(Species) %>%
      mutate(mean = mean(Petal.Length),
             se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
      ungroup() %>%
      ggplot(aes(x=Petal.Length, y=Species)) +
      stat_slab(aes(fill = Species)) +
      stat_dots(side = "bottom", shape = 16) + 
      scale_fill_brewer(palette = "Set1") +
      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=2.5) +
      theme_bw(base_size = 16) +
      theme(legend.position = "top")+
      # scale_x_continuous(limits = c(-4.5, .5), breaks = seq(-4.5, 0.5, by = 0.5))+
      labs(title="Raincloud plot with ggdist")
    

    Created on 2023-03-07 with reprex v2.0.2