3

I have a data set in R which I want to get an error bar on, however it isn't plotting correctly (see photo). I have also included my data set.

ant.d<-structure(list(group.name = structure(c(1L, 18L, 20L, 24L, 8L, 
                                        13L, 15L, 17L, 12L, 19L, 21L, 22L, 23L, 9L, 11L, 16L, 2L, 3L, 
                                        4L, 5L, 6L, 7L, 10L, 14L), .Label = c("group 1", "group 10", 
                                                                              "group 11", "group 12", "group 13", "group 14", "group 15 ", 
                                                                              "group 16 ", "group 17", "group 18", "group 19", "group 2", "group 20", 
                                                                              "group 21", "group 22", "group 23", "group 24", "group 3", "group 4 ", 
                                                                              "group 5 ", "group 6", "group 7 ", "group 8 ", "group 9 "), class = "factor"), 
               habitat.type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                          2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                          1L), .Label = c("edge", "forest", "Pasture"), class = "factor"), 
               species.richness = c(3L, 5L, 2L, 3L, 1L, 2L, 4L, 3L, 9L, 
                                    5L, 5L, 4L, 4L, 4L, 8L, 7L, 4L, 3L, 5L, 2L, 3L, 6L, 2L, 1L
               ), X = c(2.875, 2.875, 2.875, 2.875, 2.875, 2.875, 2.875, 
                        2.875, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 3.25, 
                        3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25), se = c(2.32340059786604, 
                                                                          1.7996983644207, 2.84557296642458, 2.32340059786604, 4.02424788183988, 
                                                                          2.84557296642458, 2.01212394091994, 2.32340059786604, 1.34141596061329, 
                                                                          1.7996983644207, 1.7996983644207, 2.01212394091994, 2.01212394091994, 
                                                                          2.01212394091994, 1.42278648321229, 1.52102272991811, 2.01212394091994, 
                                                                          2.32340059786604, 1.7996983644207, 2.84557296642458, 2.32340059786604, 
                                                                          1.64289231816395, 2.84557296642458, 4.02424788183988)), .Names = c("group.name", 
                                                                                                                                             "habitat.type", "species.richness", "X", "se"), row.names = c(NA, 
                                                                                                                                                                                                           -24L), class = "data.frame")

bar plot

What am I doing wrong? I've spent some time reading about error bars in R and I've not been successful.

ant.d$se <- 1.96*(sd(ant.d$species.richness, na.rm=T)/sqrt(ant.d$species.richness))

p<-ggplot(data = ant.d, aes(y = species.richness, x = habitat.type)) +
  geom_bar(stat="identity",position="dodge")
p
p + geom_bar(position=dodge) + geom_errorbar(aes(ymax = species.richness + se, ymin=species.richness - se), position=dodge, width=0.25)
Heroka
  • 12,889
  • 1
  • 28
  • 38
RyanMe321
  • 177
  • 3
  • 11
  • 1
    Don't post data as a screenshot. Please learn how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) – Jaap Sep 18 '15 at 05:16
  • 1
    Please use `dput()` function to include your data. A picture is useless. –  Sep 18 '15 at 05:16
  • Wasn't aware that it dput() existed thank you. – RyanMe321 Sep 18 '15 at 05:34

1 Answers1

2

If I understand you correctly about what you are trying to achieve, then it's probably best to aggregate your data before plotting:

df <- aggregate(cbind(species.richness,se) ~ habitat.type, ant.d, mean)

ggplot(data = df, aes(x = habitat.type, y = species.richness)) +
  geom_bar(stat="identity", fill="grey") + 
  geom_errorbar(stat="identity", aes(ymax = species.richness + se, ymin=species.richness - se), width=0.25)

which gives:

enter image description here


If you want groups within each habitat.type, you could something like this:

ggplot(data = ant.d, aes(x = habitat.type, y = species.richness, fill = group.name)) +
  geom_bar(stat="identity", position=position_dodge(0.8)) + 
  geom_errorbar(stat="identity", aes(ymax = species.richness + se, ymin=species.richness - se), width=0.25,
                position=position_dodge(0.8)) +
  scale_fill_discrete(guide = guide_legend(ncol=2))

which gives:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193