rggplot2ggpubr

ggbarplot top of one bar does not align with its error bar


Using ggpubr, I am trying to plot a bar graph comparing "Condition" variables and faceting by their "Geno". The plot I keep getting looks great except for the last bar (on the far right), where the mean_se bar is lying in the middle of the shaded area.

The plot looks like this:

This is my code:

plot = ggbarplot(filtered_df, 
                 x = "Condition", 
                 y = "ScrapeNitrite", 
                 fill = "Condition",
                 add = c("mean_se", "jitter"),
                 facet.by="Geno",
                 palette = c("white", "grey", "darkgrey","black")) +
  scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
  theme_prism()

And this is my filtered_df data:

# A tibble: 16 × 3
   Condition ScrapeNitrite Geno 
   <fct>             <dbl> <fct>
 1 naïve             16.9  FF   
 2 naïve              9.28 FF   
 3 DSS               15.2  dIEC 
 4 DSS               23.6  FF   
 5 DSS               48.8  FF   
 6 DSS               37.7  FF   
 7 DSS               34.7  FF   
 8 DSS               35.7  FF   
 9 naïve              5.09 dIEC 
10 DSS               35.8  dIEC 
11 DSS                6.33 dIEC 
12 DSS              151.   dIEC 
13 DSS               13.5  dIEC 
14 DSS               -2.84 dIEC 
15 DSS               20.8  dIEC 
16 naïve             28.2  FF   

Solution

  • The issue is that using limits=c(0, 100) in scale_y_continuous you are dropping the 2 values or observations which fall outside of the limits. Hence, these values are not taken into account when computing the the error bars. Instead you could set the limits via the coord if you only want to the hide the "outliers".

    filtered_df <- data.frame(
      Condition = factor(
        c("naïve", "naïve", "DSS", "DSS", "DSS", "DSS", "DSS", "DSS", "naïve", "DSS", "DSS", "DSS", "DSS", "DSS", "DSS", "naïve"),
        c("naïve", "DSS")
      ),
      ScrapeNitrite = c(16.9, 9.28, 15.2, 23.6, 48.8, 37.7, 34.7, 35.7, 5.09, 35.8, 6.33, 151, 13.5, -2.84, 20.8, 28.2),
      Geno = factor(
        c("FF", "FF", "dIEC", "FF", "FF", "FF", "FF", "FF", "dIEC", "dIEC", "dIEC", "dIEC", "dIEC", "dIEC", "dIEC", "FF"),
        c("FF", "dIEC")
      )
    )
    
    library(ggpubr)
    #> Loading required package: ggplot2
    
    ggbarplot(filtered_df,
      x = "Condition",
      y = "ScrapeNitrite",
      fill = "Condition",
      add = c("mean_se", "jitter"),
      facet.by = "Geno",
      palette = c("white", "grey", "darkgrey", "black")
    ) +
      scale_y_continuous(expand = c(0, 0)) +
      coord_cartesian(ylim = c(0, 100))
    #> Warning in stats::qt(ci/2 + 0.5, data_sum$length - 1): NaNs produced