I'm trying to update and plot multiple curves in a plot by using a for loop. However I can not find a way to do it in a multiple plot, using the par function. I have tried using the par function with the argument: new = TRUE. That worked with one plot and the plot contains multiple curves in one figure. However, when I'm trying to do this with multiple plots I fail. Any suggestions?
Here is a sample of my code: Im fitting 2 parametr weibull curves with multiple parameters from the vectors: boot.shape.vec and boot.scale.vec
boot.shape.vec <- c(0.7,0.6,0.8,0.5,0.65)
boot.scale.vec <- c(5,4.5,6,5.3,4.9)
#many curves
for (ii in 1:length(boot.shape.vec[1:10]))
{
for (jj in 1:length(boot.scale.vec[1:10]))
{
par(new= TRUE, mfrow=c(1,4),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat.
curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=40, col='grey70', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=40, col='darkorange', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
# h(t), the hazard function
curve((scale/intercept)*(x/boot.shape.vec[ii])^(boot.scale.vec[jj]-1), from=0,to=20,ylab=expression(hat(h)(t)),bty='n', col="darkgreen",xlab="t", lwd=0.5)
curve(dweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj])
/pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=40, col='blue', lwd=0.5, ylab=expression(hat(H)(t)), xlab='t',bty='n')
}
}
I can produce a wanted output with just one curve:
for (ii in 1:length(boot.shape.vec))
{
for (jj in 1:length(boot.scale.vec))
{
par(new= TRUE, mfrow=c(1,1),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat.
curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=100, col='grey70', lwd=1, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
}
}
#################update of Rolands proposal
Se attached code and figure, I would like all plots to include the multiple curves. Now only the last one contains the wanted output.
boot.shape.vec <- xfit.boot$estim$shape
boot.scale.vec <- xfit.boot$estim$scale
par( new=TRUE,mfrow=c(1,4),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat.
curve(pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE),
from=0, to=40,add= FALSE, col='grey70', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
curve(pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE),
from=0, to=40,add= FALSE, col='darkorange', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
# h(t), the hazard function
curve((scale/intercept)*(x/boot.shape.vec[1])^(boot.scale.vec[1]-1), from=0,to=20,ylab=expression(hat(h)(t)),bty='n',
add= FALSE,col="darkgreen",xlab="t", lwd=0.5)
curve(dweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1])
/pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE),
from=0, to=40, add= FALSE,col='blue', lwd=0.5, ylab=expression(hat(H)(t)), xlab='t',bty='n')
#many curves
for (ii in 2:length(boot.shape.vec))
{
for (jj in 2:length(boot.scale.vec))
{
#par( new=TRUE,mfrow=c(1,4),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat.
curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=40,add= TRUE, col='grey70', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=40,add= TRUE, col='darkorange', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))
# h(t), the hazard function
curve((scale/intercept)*(x/boot.shape.vec[ii])^(boot.scale.vec[jj]-1), from=0,to=20,ylab=expression(hat(h)(t)),bty='n',
add= TRUE,col="darkgreen",xlab="t", lwd=0.5)
curve(dweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj])
/pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE),
from=0, to=40, add= TRUE,col='blue', lwd=0.5, ylab=expression(hat(H)(t)), xlab='t',bty='n')
}
}