有没有办法在 R 中的特定子图中添加一行

Is there a way to add a line to a specific subplot in R

我是 R 的新手,想了解绘图的工作原理。

我正在尝试绘制一个由三个子图组成的图。我使用 par(mfrow=c(1, 3)) 来处理这个问题。但是在一个循环中,我想在三个图中添加不同的线条。如何选择将 lines 命令应用于三个子图中的哪一个?

例如,请参见下面的代码。所有 lines 命令都应用于​​第三个子图,但这当然不是我们想要的。另请参阅命令 # apply lines to first figure! but how? 和 # apply lines to second figure!但是如何?

set.seed(1)
n <- 100
x <- seq(0, 4, length.out = n) 
no_datasets <- 50

par(mfrow=c(1, 3))
for (i in 1:no_datasets) {
  x <- seq(0, 4, length.out = n) # x <- seq(-pi, pi, length.out = n)
  y <- sin(x)
  errs <- rnorm(n, mean = 0, sd = 0.25) # rnorm(n) generates random numbers whose distribution is normal
  t <- y + errs

  df <- data.frame(x, y, t, errs)

  model1 <- lm(t ~ poly(x, 1), data = df)
  model5 <- lm(t ~ poly(x, 5), data = df)
  model25 <- lm(t ~ poly(x, 25), data = df)

  if (i == 1) {
    plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")
    plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")
    plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")
  }
  t_hat <- predict(model1, df)
  # apply lines to first figure! but how?
  lines(df$x, t_hat, col="blue")

  t_hat <- predict(model5, df)
  # apply lines to second figure! but how?
  lines(df$x, t_hat, col="blue")


  t_hat <- predict(model25, df)
  # apply lines to third figure!
  lines(df$x, t_hat, col="blue")
}

由于第三个情节是最后一个,所以它只是将所有线条添加到最后一个情节。但是,如果您将所有内容都嵌套在每个图的 if 语句中,那么您将在每个图上得到线条。

set.seed(1)
n <- 100
x <- seq(0, 4, length.out = n) 
no_datasets <- 50

par(mfrow=c(1, 3))

for (i in 1:no_datasets) {
  x <- seq(0, 4, length.out = n) # x <- seq(-pi, pi, length.out = n)
  y <- sin(x)
  errs <- rnorm(n, mean = 0, sd = 0.25) # rnorm(n) generates random numbers whose distribution is normal
  t <- y + errs
  
  df <- data.frame(x, y, t, errs)
  
  model1 <- lm(t ~ poly(x, 1), data = df)
  model5 <- lm(t ~ poly(x, 5), data = df)
  model25 <- lm(t ~ poly(x, 25), data = df)
  
  if (i == 1) {
    plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")
    
    t_hat <- predict(model1, df)
    lines(df$x, t_hat, col="blue")
  }
  
  if (i == 2) {
    plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")
    
    t_hat <- predict(model5, df)
    lines(df$x, t_hat, col="blue")
  }
  
  if (i == 3) {
    plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")
    
    t_hat <- predict(model25, df)
    lines(df$x, t_hat, col="blue")
  }  
  
}

好吧,如果我理解AndrewGB,那是不可能的。那么答案应该是:

set.seed(1)
n <- 100 
no_datasets <- 50

par(mfrow=c(1, 3))
polynomials <- c(1, 5, 25)

x <- seq(0, 4, length.out = n) # x <- seq(-pi, pi, length.out = n)
y <- sin(x)

for (i in 1:length(polynomials)) {
  degree <- polynomials[i]
  for (j in 1:no_datasets) {
    errs <- rnorm(n, mean = 0, sd = 0.25) # rnorm(n) generates random numbers whose distribution is normal
    t <- y + errs
    df <- data.frame(x, y, t, errs)



    model <- lm(t ~ poly(x, degree), data = df)
    if (j == 1) {
      plot(df$x, df$y, xlab = "x", ylab="", col="black", type="l")  
    }
    t_hat <- predict(model, df)
    lines(df$x, t_hat, col="blue")
  }
}