r - 将误差线添加到多条线上以在 R 中的绘图上显示标准差

标签 r plot errorbar

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

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功能但没有成功。

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 是一个向量化函数。因此有可能避免 maply 调用。考虑一下(我还用 matplot 替换了您的第一个 mapply 调用):

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

enter image description here

关于r - 将误差线添加到多条线上以在 R 中的绘图上显示标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40384055/

相关文章:

r - 创建列的参数化摘要

javascript - 在 Dygraphs 中组合 scatter 和 customBars

r - 将误差线添加到 plotly box plot 中的点

r - 处理非常大的 expand.grid?

r - 插入符 - 使用 train()、predict() 和 resamples() 的不同结果

r - 检查差异是否小于机器精度的正确/标准方法是什么?

r - R 中的多个直方图

R: map 上的颜色点基于它们的值(value)并添加图例

python - 如何使用 CDS 列设置多行字形的 "line_dash"?

r - 使用 ggplot 和 facet_grid 指定误差线