geom_errorbar with position="dodge" not working

Hello,

I have managed to produce error bars from SD for my barplots when using only one variable, but once I try to include error bars for a data set with two variables, using position="dodge" and fill="variable", the error bars do not work.

I have included the ggplot code I have been using below as well as an image of the graph and some of the raw data in my table. Is there a way I can separate the error bars to be specific to either the Iron or no iron columns?

ggplot(data=Initial_iron_experiments, mapping=aes(x=Sample, y=Percentage_killing, fill=Iron))+
stat_summary(fun=mean, geom="col", position="dodge", colour="black")+
geom_errorbar(aes(x=Sample, ymin=mean(Percentage_killing)-SD, ymax=mean(Percentage_killing)+SD), width=0.5, colour="black", alpha=0.5, size=0.5)+
scale_y_continuous("Percentage killing", breaks=c(0,10,20,30,40,50,60,70,80,90,100))+
labs(title="Killing effect of P.aeruginosa on R.microsporus")+
theme(plot.title=element_text(hjust=0.5))

You need to dodge the error bar, as well as the barplot.

Initial_iron_experiments %>%
ggplot(mapping=aes(x=Sample, y=Percentage_killing, fill=Iron)) +
  geom_bar(position=position_dodge(), stat="identity", colour='black') +
  geom_errorbar(aes(x=Sample, ymin=mean(Percentage_killing)-SD, ymax=mean(Percentage_killing)+SD), width=.2, position=position_dodge(.9))

Or something like that. It would be easier with a repex to mess about with some data.

dput(head(Initial_iron_experiments , n=30))

Thank you, that moved them into the right positions horizontally. I now have the issue of them not being correctly positioned vertically, with some of them not being anywhere near the plotted value. In the code I've got it should be instructing the error bars to be placed 1 SD above the mean and 1 SD below the mean, but as you can see, some of them are just positioned in the middle of the bar. It seems they are all based upon the same mean value, so how would I adjust this so each error bar is specific to each bar?

ggplot(data=Initial_iron_experiments, mapping=aes(x=Sample, y=Percentage_killing, fill=Iron))+
  stat_summary(fun=mean, geom="col", position=position_dodge(), colour="black")+
  geom_errorbar(aes(x=Sample, ymin=mean(Percentage_killing)-SD, ymax=mean(Percentage_killing)+SD), position=position_dodge(0.9), width=0.5, colour="black", alpha=0.5, size=0.5)+
  scale_y_continuous("Percentage killing", breaks=c(0,10,20,30,40,50,60,70,80,90,100))+
  labs(title="Killing effect of P.aeruginosa on R.microsporus")+
  theme(plot.title=element_text(hjust=0.5))

I'm not convinced you need stat_summary, if you follow the link here, you will a number of examples or barplots with error bar that may give you a better idea of how to code the graph. This link is perhaps closer to your example.

When I didn't use stat_summary before it caused the bars to look like they do in the picture below, as it was plotting all of the raw data for each variable on top of each other, so I used stat_summary in order to only plot the mean data values. Would there not be a way, whilst using stat_summary, to have the error bars coming from a different value for each bar?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.