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)
您当然可以在段调用中添加任何内容,但是我不确定您要问什么。首先,我们可以大大简化您的代码:
这将产生以下图:
You could replace
yval
withyval+.1
in thesegments
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.