# 增加基座R中线段之间的垂直空间

I wrote a function that plots a matrix of confidence intervals, and I am trying to increase the space between each segment for each successive interval to avoid overplotting. This means I will have to set my `ylim` to accommodate the number of intervals in the plot, but I am not sure how to approach this so that it will increment vertically in the loop.

``````
set.seed(200)
x <- rnorm(100, 10)
truemean <- mean(x)

mat <- replicate(100, t.test(sample(x, rep = T))\$conf.int)
mat <- t(mat)

myfunc <- function(mat, truemean) {

plot(x = c(min(mat[ , 1]), max(mat[ , 2])),
y = c(1, 100),
type = "n",
xlab = "0:100",
ylab = "0:100")

abline(v = truemean)

for (i in 1:nrow(mat)) {

if (mat[i, 1] <= truemean & mat[i, 2] >= truemean) {

segments(x0 = mat[i, 1], y0 = i,
x1 = mat[i, 2], y1 = i,
col = "blue",
lwd = 2)

} else {

segments(x0 = mat[i, 1], y0 = i,
x1 = mat[i, 2], y1 = i,
col = "red",
lwd = 2)

}

}

}

myfunc(mat, truemean)

``````

• 凹凸曼 回复

您当然可以在段调用中添加任何内容，但是我不确定您要问什么。首先，我们可以大大简化您的代码：

``````set.seed(200)
x <- rnorm(100, 10)
truemean <- mean(x)

mat <- replicate(100, t.test(sample(x, rep = T))\$conf.int)
mat <- t(mat)

yval <- seq(dim(mat))
clr <- ifelse(mat[, 1] <= truemean & mat[, 2] >= truemean, "blue", "red")
plot(NA, xlim=c(min(mat[ , 1]), max(mat[ , 2])), ylim=c(0, length(yval)), type="n",
xlab="Conf Int", ylab="Trials")
abline(v=truemean)
segments(mat[, 1], yval, mat[, 2], yval, col=clr)
``````

这将产生以下图：

You could replace `yval` with `yval+.1` in the `segments` function to shift everything up. If there are so many lines that they overlap you can increase the height of the plot to make more room.