Home > Software design >  My errorbars for geombar plot are messed up despite my position dodge
My errorbars for geombar plot are messed up despite my position dodge

Time:09-29

My error bars in my barplot are messed up despite the position dodge, I used the same for a less complicated plot and it worked. I welcome suggestions around itenter image description here

tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Visible", "Remembered"), class = "factor"), 
    Condition = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 
    2L, 3L, 3L), .Label = c("CEN", "IPS", "CTR"), class = "factor"), 
    test = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L), .Label = c("Pretest", "Posttest"), class = "factor"), 
    N = c(12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12), EE = c(7.33336873483333, 
    6.80361546108333, 7.09299323975, 7.85694358425, 7.03401583625, 
    6.98843623408333, 6.64690423166667, 6.76286593966667, 6.53229116175, 
    6.597801229, 5.87755052541667, 6.29406939166667), sd = c(1.10209636894719, 
    0.640166385925573, 0.829194321849813, 0.815786383997366, 
    1.13990647741682, 0.831699837406531, 0.894188346279884, 0.836594325568601, 
    0.762116322320573, 0.810426854086389, 0.986911196860133, 
    0.835898962602035), se = c(0.318147817642284, 0.18480011762014, 
    0.239367782465249, 0.235497244201055, 0.329062655793799, 
    0.240091062505814, 0.258129941215459, 0.241503979534773, 
    0.220004031922795, 0.233950081182639, 0.284896722586727, 
    0.241303245536807), ci = c(0.700238625346207, 0.406742316465117, 
    0.526844937010058, 0.518325939729, 0.724262022142493, 0.528436865645932, 
    0.568140169990552, 0.531546675059109, 0.484225609422399, 
    0.514920656885223, 0.62705345857213, 0.531104862508461)), row.names = c(NA, 
-12L), class = "data.frame")
tgc <- summarySE(data10, measurevar="EE", groupvars=c("Group", "Condition", "test"))

ggplot(tgc, aes(x = Condition, y = EE), fill = test)  
  geom_errorbar(aes(ymin=EE-se, ymax=EE se), position = position_dodge(0.5), width=.1)  
  geom_bar(aes(fill = test), stat = "identity", width = 0.5, color = "black", position='dodge')   ylim(0,9)   theme_bw()  
  geom_signif(data = data.frame(Condition = c("CEN","IPS", "CTL")),
              aes(y_position=c(8.5, 8.5, 8.5, 8.5, 8.5, 8.5), xmin=c(0.8, 1.8, 2.8, 0.8, 1.8, 2.8), xmax=c(1.2, 2.2, 3.2, 1.2, 2.2,3.2),
                  annotations=c("**", "*", "NS", "*", "**", "NS")), tip_length=0, manual = T)  
  scale_fill_manual(values = c("grey80", "grey20"))  
  facet_grid(~ Group, scales = "free")

CodePudding user response:

You need to let ggplot know which variable to use as the grouping variable on which to apply the dodge (in your case this would be test). Also, you should draw the error bars after the actual bars so that the lower portion remains visible:

ggplot(tgc, aes(x = Condition, y = EE, fill = test))  
  geom_bar(stat = "identity", width = 0.5, color = "black", position='dodge')   
  geom_errorbar(aes(ymin = EE - se, ymax = EE   se, group = test), 
                position = position_dodge(width = 0.5), width = 0.25)  
  geom_signif(data = data.frame(Condition = c("CEN","IPS", "CTL"),
                              ypos      = c(8.5, 8.5, 8.5, 8.5, 8.5, 8.5),
                              xmin      = c(0.8, 1.8, 2.8, 0.8, 1.8, 2.8),
                              xmax      = c(1.2, 2.2, 3.2, 1.2, 2.2,3.2),
                              annot     = c("**", "*", "NS", "*", "**", "NS")),
            mapping = aes(y_position = ypos, 
                          xmin = xmin, 
                          xmax = xmax,
                          annotations = annot),
            tip_length = 0, manual = TRUE, inherit.aes = FALSE)
  scale_fill_manual(values = c("grey80", "grey20"))  
  facet_grid(~ Group, scales = "free")  
  ylim(0, 9)   
  theme_bw()

enter image description here

  • Related