How to add mean +/- 95%CI error bars to violin plot

ggplot(five,aes(x=Derivative,y=Score))+geom_violin()+scale_x_discrete(limits=c("None","134","138"))+geom_dotplot(binaxis="y",stackdir="center",dotsize=0.75)+ylim(0,5)+ylab("Score")+xlab("TLR4 Antagonist")

This is what I have so far. How do I add mean data point +/- 95%CI error bars inside violin plot?

Here is an example of showing the distribution mean and 95% CI in a violin plot. Note that I did not do an exact calculation of the confidence interval.

library(ggplot2)
library(dplyr)

DF <- data.frame(GRP = rep(c("A", "B"), each = 50), Value = c(rnorm(50), rnorm(50, 1, 1)))
Stats <- DF %>% group_by(GRP) %>% summarize(Mean = mean(Value), SD = sd(Value),
                                            CI_L = Mean - (SD * 1.96)/sqrt(50),
                                            CI_U = Mean + (SD * 1.96)/sqrt(50))
Stats  
#> # A tibble: 2 x 5
#>   GRP    Mean    SD   CI_L  CI_U
#>   <fct> <dbl> <dbl>  <dbl> <dbl>
#> 1 A     0.131 0.962 -0.135 0.398
#> 2 B     1.12  1.10   0.815 1.43
ggplot() + geom_violin(data = DF, mapping = aes(GRP, Value)) +
  geom_point(mapping = aes(GRP, Mean), data = Stats) +
  geom_errorbar(mapping = aes(x = GRP, ymin = CI_L, ymax = CI_U), 
                data = Stats, width = 0.2)

Created on 2020-03-22 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.