How to you generate error bars on barplot containing two data sets?

余生颓废 提交于 2019-12-24 11:37:25

问题


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:


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)


来源:https://stackoverflow.com/questions/30967643/how-to-you-generate-error-bars-on-barplot-containing-two-data-sets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!