如何在包含两个数据集的条形图上生成误差线? [英] How to you generate error bars on barplot containing two data sets?

查看:30
本文介绍了如何在包含两个数据集的条形图上生成误差线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成了如下所示的条形图,目前正在尝试添加误差条.我使用了两列数据来生成沿 x 轴显示的条形图.是否可以使用标准偏差/标准误差值向 x 轴上的这两个数据集的每一个添加误差线,如果是这样,我该怎么做?我已经添加了一些我的原始数据和代码以帮助更有意义,并且一直在尝试使用箭头函数来添加误差线,但是我没有想出两个来为吸引和不吸引做这件事酒吧.

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.

在此先感谢您的帮助.

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"))

推荐答案

在你的情节之后,使用segments:

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)

这篇关于如何在包含两个数据集的条形图上生成误差线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆