r - 添加新的回归线,但保留 R 中先前运行的回归线

标签 r layout plot bootstrapping par

背景

我有一个名为 TPN 的函数(R 代码在图片下方)。当您运行此函数时,它会生成两个图(参见下图)。底行图从顶行图采样,然后添加红色回归线。每次运行TPN函数时, 底行图生成一条新的红色回归线

问题

底行图中,我想知道每次运行TPN函数时是否有一种方法可以保留之前运行的回归线(< strong>见下图)?

也就是说,每次我运行一个新的 TPN 函数时,前一次运行的回归线都保留在它的位置(为了区分目的,可能使用“红色”以外的其他颜色),新的回归线刚刚添加到底行图中?

enter image description here

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

这是输出:

enter image description here

关于r - 添加新的回归线,但保留 R 中先前运行的回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44033567/

相关文章:

r - R中的mclapply禁止显示警告

regex - 检查字符值是否是有效的 R 对象名称

r - seq 和 seq_along,两全其美?

android - 在Android中,如何在不拉伸(stretch)的情况下将图像放在按钮的中心?

python - 更改 Altair 图中的图例编号范围

matlab - 标记不同的图形、字体、大小 MATLAB

r - 如何在 R 中重置 par(mfrow)

r - 使用 .Rbuildignore 忽略给定类型的所有文件

css - 带有重叠 div 的 IE7 CSS 问题

html - 整个页面上没有垂直滚动条的标题、导航、容器布局