-1

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')

  }
}

enter image description here

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))


  }
}

enter image description here

#################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')

  }
}

enter image description here

jonas
  • 13,559
  • 22
  • 57
  • 75

1 Answers1

1
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)

par(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[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE), 
      from=0, to=100, col='grey70', lwd=1, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1))

for (ii in 2:length(boot.shape.vec))
{
  for (jj in 2:length(boot.scale.vec))
  {
    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), 
          add = TRUE)
  }
}

resulting plot

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Yes, this one I managed to perform, however using the par function and multiple plot is my problem.. – jonas Apr 27 '17 at 10:09
  • Then, I don't understand what you want to achieve. If you remove `add = TRUE` from my code, you get multiple plots. – Roland Apr 27 '17 at 10:12