2
votes

I have generated that barplot shown below and am currently attempting to add error bars. I've used two columns of data to produce the bars shown along the x-axis. Is it possible to add error bars using standard devation/standard error values to each of those two data sets on the x-axis and if so, how can I do this? I've added some of my raw data and code to help make more sense, and have been attempting to use the arrows function in order to add the error bars, but am failing to figure out two to do this for both attracted and not attracted bars.

Thanks in advance for any help.

Species         Not attracted   Number attracted
Atlantic cod       92           0
Haddock             0           0
Whiting             0           0
Haddock             0           0
Whiting             0           0
Atlantic cod        2           0
Haddock             0           0
Whiting             0           1

meanMNAtt <- tapply(MaxN$Number.attracted, list(MaxN$Species), mean)
sdMNAtt<- tapply(MaxN$Number.attracted, list(MaxN$Species), sd)
meanMNnotAtt <- tapply(MaxN$Not.attracted, list(MaxN$Species), mean)
sdNA <- tapply(MaxN$Not.attracted, list(MaxN$Species), sd)

MN_mean <- matrix(c(0.02083333, 0.89583333, 1.41666667, 2.770833, 1.083333,     0.125000), 2, 3, byrow = TRUE, dimnames = list(c("Attracted", "Not Attracted"), c("Atlantic cod", "Haddock", "Whiting")))

MN_sd <- matrix(c(0.1443376, 1.9267389, 2.5751127, 13.3372612, 2.8346889, 0.3927535), 2, 3, byrow = TRUE, dimnames = list(c("SD_Att", "SD_NAtt"), c("Atlantic cod", "Haddock", "Whiting")))

SDPlot <- barplot(MN_mean, beside=TRUE, ylim=c(0, 5), xlab="Species", ylab="Attraction at MaxN", axes=TRUE, las=1, col = c("Black", "Gray60"))
1

1 Answers

0
votes

after your plot, add it in using segments:

segments(x0=SDPlot, y0=MN_mean-MN_sd, x1=SDPlot, y1=MN_mean+MN_sd, lwd=2)

If you also want horizontal ends:

segments(x0=SDPlot-0.1, y0=MN_mean-MN_sd, x1=SDPlot+0.1, y1=MN_mean-MN_sd, lwd=2)
segments(x0=SDPlot-0.1, y0=MN_mean+MN_sd, x1=SDPlot+0.1, y1=MN_mean+MN_sd, lwd=2)