Adding error bars to geom_col from a dataframe

39 Views Asked by At

I am trying to add error bars to the plot of the following data frame:

Village <- as.factor(c("Beng-Gaty", "Beng-Gaty", "Ohchra", "Ohchra", "Ohtrone", "Ohtrone", "Sraevly", "Sraevly"))
Prevalence_Measure <- as.factor(c("Crude", "Adjusted", "Crude", "Adjusted", "Crude", "Adjusted", "Crude", "Adjusted"))
Prevalence <- as.numeric(c(37.7, 42.3, 39.5, 43.9, 19.4, 30, 18.2, 21.2))
Lower_CI <- as.numeric(c(0.285, 0.329, 0.316, 0.360, 0.123, 0.215, 0.124, 0.152))
Upper_CI <- as.numeric(c(0.476, 0.524, 0.477, 0.524, 0.283, 0.399, 0.252, 0.288))

Village_Prevalences_Comp <- data.frame(Village, Prevalence_Measure, Prevalence, Lower_CI, Upper_CI)

I have tried both of the following and looked online - but it is still baffling me:

The first two efforts did not "dodge" the error bars and seemed to have only one error bar (in the wrong place), where there should have been two.

Village_Prevalences_Comp %>%
  ggplot() +
  geom_col( 
    mapping = aes(
      x=Village, 
      fill=Prevalence_Measure,
      y=Prevalence),
    position = "dodge") +
  geom_pointrange(aes(x=Village, y=Prevalence, ymin=Lower_CI, ymax=Upper_CI))
  
Village_Prevalences_Comp %>%
  ggplot(aes(x=Village, fill=Prevalence_Measure, y=Prevalence)) +
  geom_col(position = "dodge") +
  geom_line(ymin=Lower_CI, ymax=Upper_CI)

The third example, here, had two error bars, but they were at the y = 0 point on the graph.

Village_Prevalences_Comp %>%
  ggplot() +
  geom_col( 
    mapping = aes(
      x=Village, 
      fill=Prevalence_Measure,
      y=Prevalence),
    position = "dodge") +
  geom_errorbar(data = Village_Prevalences_Comp, aes(x=Village, ymin=Lower_CI, ymax=Upper_CI))

Thanks

1

There are 1 best solutions below

0
On

I was going to delete my question after realising my silly mistake, but then thought there could be others like me that are making the same silly mistake.

I realised my Upper and Lower confidence intervals were all given as proportions and not percentages, so of course the error bars were squeezed at y=0.

The following code now works

Prevalence_Measure <- as.factor(c("Crude", "Adjusted", "Crude", "Adjusted", "Crude", "Adjusted", "Crude", "Adjusted"))
Prevalence <- as.numeric(c(37.7, 42.3, 39.5, 43.9, 19.4, 30, 18.2, 21.2))
Lower_CI <- as.numeric(c(28.5, 32.9, 31.6, 36.0, 12.3, 21.5, 12.4, 15.2))
Upper_CI <- as.numeric(c(47.6, 52.4, 47.7, 52.4, 28.3, 39.9, 25.2, 28.8))

Village_Prevalences_Comp <- data.frame(Village, Prevalence_Measure, Prevalence, Lower_CI, Upper_CI)

ggplot(Village_Prevalences_Comp, aes(x = Village, y = Prevalence, fill = Prevalence_Measure)) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = Lower_CI, ymax = Upper_CI),
                position = position_dodge(width = 0.9), width = .2)