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
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