使用基本循环,您可以尝试以下操作:
for(i in 1:length(N)) {
add <- if(i == 1) FALSE else TRUE
curve( df(f[i], df1[i], df2[i], ncp = (x * N[i]) / (1 - x) ) ,
col = i, lwd = 3, from = 0, to = 1, n = 1e4, add=add)
}
不是最漂亮的,而是使用基础知识。你绝对应该探索ggplot2。
为避免某些曲线不完整,您可以先获取每条曲线的值,估计它们的范围,然后绘制它们。例如:
# function to get the values of each curve
curve_ <- function(x, i) df(f[i], df1[i], df2[i], ncp = (x * N[i]) / (1 - x))
# estimate the values of each curve
curves <- lapply(1:length(N), function(i) {
x <- seq(0,1,length.out = 1e4)
curve_(x, i)
})
# estimate the ranges
ranges_ <- sapply(curves, range, na.rm=T)
# plot using the ranges as plotting parameters (ylim)
for (i in 1:length(curves)) {
if(i == 1) {
plot(curves[[i]], type='l', col=i, xlim=c(0,length(curves[[i]])), ylim=c(min(ranges_[1,]), max(ranges_[2,])))
} else {
lines(curves[[i]], col=i)
}
}