arrows
是矢量化函数。所以有可能避免mapply
调用。考虑(我也用 替换了你的第一个mapply
电话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)
![在此处输入图像描述](https://i.stack.imgur.com/alAb7.jpg)