displacing of error bar use ggbarplot from ggpubr package, when there are NA

24 Views Asked by At

The codes work when there are observations for all group in the dataset. However, when a group of data is NA, the error bars will be displaced. Here is an example:

library(ggpubr)
# Load the ToothGrowth dataset
data(ToothGrowth)
ggbarplot(ToothGrowth, x = "dose", y = "len", 
   add = "mean_se", 
   color = "supp", palette = "jco",
   position = position_dodge(0.8)
) + 
  ggtitle("no NA in dataset")

enter image description here

# Motified dataset
ToothGrowth$len[ToothGrowth$dose == 2 & ToothGrowth$supp == "VC"] <- NA

head(ToothGrowth)
ggbarplot(ToothGrowth, x = "dose", y = "len", 
   add = "mean_se", 
   color = "supp", palette = "jco",
   position = position_dodge(0.8)
)+
  ggtitle("one group is NA")

enter image description here

1

There are 1 best solutions below

0
stefan On BEST ANSWER

You could fix your issue by setting preserve = "single" in position_dodge():

library(ggpubr)
#> Loading required package: ggplot2

ToothGrowth$len[ToothGrowth$dose == 2 & ToothGrowth$supp == "VC"] <- NA

ggbarplot(ToothGrowth,
  x = "dose", y = "len",
  add = "mean_se",
  color = "supp", palette = "jco",
  position = position_dodge(0.8, preserve = "single")
) +
  ggtitle("one group is NA")