0

I am simply trying to add error bars to a side by side bar plot using ggplot. I think data is arranged correctly as per below. I want to graph differences in fecundity between inbred and outbred populations derived from populations with (+SS) and without (-SS) sexual selection.

       inbreeding  SS       Fecundity      se
1      Inbred      +SS      5.60      0.8596205 
2      Inbred      +SS      7.40      1.1639316 
3      Inbred      +SS      6.25      1.2457824 
4      Inbred      +SS      1.40      0.1854050 
5     Outbred      +SS      7.70      1.2377824 
6     Outbred      +SS      6.30      0.6613384 
7     Outbred      +SS      2.35      1.0137865 
8     Outbred      +SS      8.27      1.2775966 
9      Inbred      -SS      9.15      1.7595977 
10     Inbred      -SS     12.50      1.7464249 
11     Inbred      -SS     10.95      1.9063260 
12     Inbred      -SS      3.65      1.2036676 
13    Outbred      -SS      7.65      1.5564382 
14    Outbred      -SS      9.10      1.5250539 
15    Outbred      -SS      5.75      1.3315503
16    Outbred      -SS      3.65      0.9821432

The code I am using for making the plot is

ggplot(Inbreeding27Means.ggplot,aes(x=SS,y=Fecundity,fill=inbreeding))+
geom_bar(stat="identity",position=position_dodge())+
geom_errorbar(aes(ymin=Fecundity-se,ymax=Fecundity+se),width=.2,
  position=position_dodge(.9))

Which generally came from the R cookbook website here

What appears to happen is that multiple error bars within each column/bar are added for each group of my grouping variable (inbreeding) ( I think). Sorry my reputation is not quiet high enough to add a picture. So I hope code is clear. The side by side bar graphs on the R cookbook website is essentially what I am aiming for except my variable (SS) on x-axis has only two groups.

Obviously I'd like to just have one error bar per bar on the plot. If someone could suggest what I may have done wrong here i'd much appreciate it.

1 Answer 1

2

Comparing with the example from R cookbook, your data has duplicate entries, i.e. each SS and inbreeding combination is not unique as you have four each. What is the numeric value in the first column? Is it something like a second treatment variable, i.e. do you expect there to be 16 bars? Then you would want to look for something like this:

data$population = c(1:16)
data$treatment = paste(data$population,data$inbreeding)

g=ggplot(data,aes(x=as.factor(population),y=Fecundity,fill=as.factor(SS),group=inbreeding))
g=g+geom_bar(stat="identity",position=position_dodge())
g=g+geom_errorbar(aes(ymin=Fecundity-se,ymax=Fecundity+se),width=.2,position=position_dodge(.9))
g=g+facet_grid(.~inbreeding,scales="free_x")
g

16 bars with error bars

If you are only looking for four bars your data set should only have 4 rows.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for pointing out the duplicate entries in the data I actually solved the problem but using just a single mean value for each group. However the plot you made above is in fact more intuitive as it allows me to look at differential inbreeding between my populations which I am also interested in, many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.