将误差线添加到多条线以在R中的图上显示标准偏差 [英] Add error bars to multiple lines to show standard deviation on a plot in R

查看:185
本文介绍了将误差线添加到多条线以在R中的图上显示标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多不同线的图,我想在每条线上的每个点上添加误差线.

I have a plot with many diferent lines and I'd like to add error bars to each point on every line.

df <- matrix(runif(25),5,5)
plot(1:5,seq(0,1,1/4),type = 'n')
mapply(lines,as.data.frame(df),col=cols,pch=1:5,type="o")

我尝试使用arrows函数,但没有成功.

I have tried to use the arrows function but with no success.

stdev <- matrix(runif(25,0,0.1),5,5)
A <- as.data.frame(df) + as.data.frame(stdev)
B <- as.data.frame(df) - as.data.frame(stdev)
mapply(arrows(1:5,A,1:5,B,col=cols,angle=90,length=0.03, code=3))

有什么建议吗?

推荐答案

arrows是矢量化函数.因此有可能避免mapply调用.考虑(我也用matplot代替了您的第一个mapply呼叫):

arrows is a vectorized function. So there is a possibility to avoid mapply call. Consider (I have also replaced your first mapply call by matplot):

## generate example data
set.seed(0)
mat <- matrix(runif(25), 5, 5)  ## data to plot
stdev <- matrix(runif(25,0,0.1), 5, 5)  ## arbitrary standard error
low <- mat - stdev  ##  lower bound
up <- mat + stdev  ## upper bound

x <- seq(0,1,1/4)  ## x-locations to plot against
## your colour setting; should have `ncol(mat)` colours
## as an example I just use `cols = 1:ncol(mat)`
cols <- 1:ncol(mat)
## plot each column of `mat` one by one (set y-axis limit appropriately)
matplot(x, mat, col = cols, pch = 1:5, type = "o", ylim = c(min(low), max(up)))
xx <- rep.int(x, ncol(mat))  ## recycle `x` for each column of `mat`
repcols <- rep(cols, each = nrow(mat))  ## recycle `col` for each row of `mat`
## adding error bars using vectorization power of `arrow`
arrows(xx, low, xx, up, col = repcols, angle = 90, length = 0.03, code = 3)

这篇关于将误差线添加到多条线以在R中的图上显示标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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