背景
我有一个称为TPN的函数( R代码在图片下方)。运行此函数时,它会生成两个图(参见下面的图片)。底部行图来自顶部行图,然后 添加到红色回归线。 每次您运行TPN函数时,底行图都会生成一条新的红色回归线

底部行情节中,我想知道是否有一种方法可以让我每次运行TPN函数时都保留上一次运行的回归线(请参见下的图片)?
也就是说,每次我运行新的TPN函数时,都会将前一次运行的回归线保留在原处(出于区分目的,可能使用“红色”以外的其他颜色),并且刚刚将新回归线添加到了他底行图?
r - 添加新的回归线,但将先前运行中的回归线保留在R中-LMLPHP

############## Input Values #################
 TPN = function(      each.sub.pop.n = 150,
                      sub.pop.means = 20:10,
                      predict.range = 10:0,
                      sub.pop.sd = .75,
                      n.sample = 2   ) {
#############################################
 par( mar = c(2, 4.1, 2.1, 2.1) )

 m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
 set.seed(2460986)
 Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')

 y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
 set.seed(NULL)
 x <- rep(predict.range, each = each.sub.pop.n)

 plot(x, y, ylim = range(y)) ## Top-Row Plot


  sample <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
  sample <- data.frame(y = unlist(sample),
                 x = as.numeric(rep(names(sample), each = n.sample)))

     x = sample$x  ;  y = sample$y

     plot(x, y, ylim = range(y))  #### BOTTOM-ROW PLOT

     abline(lm(y ~ x), col = 'red') # Regression Line

      }
      ## TEST HERE:
      TPN()

最佳答案

这不是那么容易。我做了另一个功能,也编辑了第一个功能。

总结一下我做了什么:

我制作了第一个在其末尾设置par(new = TRUE)的函数。另外,将底部行图中的点颜色设置为仅用于格式化的白色。您可以根据需要摆脱col = 'white', bg = 'white'

然后,在第二个函数中,不会绘制上排图,并且不会从每个“测试”将yaxis添加到下排图。

往下看:

############## Input Values #################
TPN = function(      each.sub.pop.n = 150,
                     sub.pop.means = 20:10,
                     predict.range = 10:0,
                     sub.pop.sd = .75,
                     n.sample = 2   ) {
  #############################################
  par( mar = c(2, 4.1, 2.1, 2.1) )

  m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
  set.seed(2460986)
  Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')

  y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
  set.seed(NULL)
  x <- rep(predict.range, each = each.sub.pop.n)

  par(new = FALSE)
  plot(x, y, ylim = range(y)) ## Top-Row Plot


  sample <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
  sample <- data.frame(y = unlist(sample),
                       x = as.numeric(rep(names(sample), each = n.sample)))

  x = sample$x  ;  y = sample$y

  plot(x, y, ylim = range(y), col = 'white', bg = 'white')  #### BOTTOM-ROW PLOT
  abline(lm(y ~ x), col = 'red') # Regression Line
  par(new = TRUE)
}

第二个不绘制第一行:
############## Input Values #################
TPN2 = function(      each.sub.pop.n = 150,
                     sub.pop.means = 20:10,
                     predict.range = 10:0,
                     sub.pop.sd = .75,
                     n.sample = 2   ) {
  #############################################
  par( mar = c(2, 4.1, 2.1, 2.1) )

  m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
  set.seed(2460986)
  Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')

  y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
  set.seed(NULL)
  x <- rep(predict.range, each = each.sub.pop.n)

  #par(new = FALSE)                           #comment-out
  #plot(x, y, ylim = range(y)) ##Top-Row Plot #comment-out


  sample <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
  sample <- data.frame(y = unlist(sample),
                       x = as.numeric(rep(names(sample), each = n.sample)))

  x = sample$x  ;  y = sample$y

  plot(x, y, ylim = range(y), axes = FALSE,  col = 'white', bg = 'white') ##BOTTOM-ROW PLOT
  abline(lm(y ~ x), col = 'blue') # Regression Line
  par(new = TRUE)
}

然后您的测试将如下所示:
## TEST HERE:
TPN()

TPN2()
TPN2()
TPN2()

这是输出:

10-02 06:53