使用par(new = T)时在R中的多个图中缩放重叠

时间:2018-12-28 02:31:35

标签: r plot

我正在尝试在一个绘图中的相同比例轴上绘制3个不同的绘图。该图进展顺利,但y轴刻度数彼此重叠。 这是我的情节。

h1<-hazard.plot.w2p(beta=beta.spreda,eta=eta.spreda,time=exa1.dat$time,line.colour="orange")
h2<-hazard.plot.w2p(beta=1.007629,eta=32.56836,time=exa1.dat$time,line.colour="red")
h3<-hazard.plot.w2p(beta=1.104483,eta=36.53923,time=exa1.dat$time,line.colour="green")
  

用于运行此代码的函数:

    hazard.plot.w2p <- function(beta, eta, time, line.colour, nincr = 500) {
  max.time <- max(time, na.rm = F)
  t <- seq(0, max.time, length.out = nincr)
  r <- numeric(length(t))
  for (i in 1:length(t)) {
    r[i] <- failure.rate.w2p(beta, eta, t[i])
  }
  plot(t, r, type = 'l', bty = 'l', 
       col = line.colour, lwd = 2, 
       main = "", xlab = "Time", 
       ylab = "Failure rate", 
       las = 1, adj = 0.5, 
       cex.axis = 0.85, cex.lab = 1.2)
par(new=TRUE)
}
  

样本数据集:

[fail       time
a          4.55
a          4.65
a          5.21
b          3.21
a          1.21
a          5.65
a          7.12][1]

这是我得到的输出:

1 个答案:

答案 0 :(得分:0)

这是一个测试,尽管我不知道它是否有效(缺少一些函数/变量):

hazard.plot.w2p <- function(beta, eta, time, line.colour, nincr = 500,
                            add = FALSE) {
  max.time <- max(time, na.rm = F)
  t <- seq(0, max.time, length.out = nincr)
  r <- failure.rate.w2p(beta, eta, t)
  if (!add) {
    plot(NA, type = 'n', bty = 'l', xlim=range(t), ylim=range(r),
         main = "", xlab = "Time", ylab = "Failure rate",
         las = 1, adj = 0.5, cex.axis = 0.85, cex.lab = 1.2)
  }
  lines(t, r, col = line.colour, lwd = 2)
}
failure.rate.w2p <- function(beta,eta,time) (beta/eta) * (time/eta)^(beta-1)

h1<-hazard.plot.w2p(beta=1.002,eta=30,time=exa1.dat$time,line.colour="orange")
h2<-hazard.plot.w2p(beta=1.007629,eta=32.56836,time=exa1.dat$time,line.colour="red",add=T)
h3<-hazard.plot.w2p(beta=1.104483,eta=36.53923,time=exa1.dat$time,line.colour="green",add=T)

single plot