Home > front end >  Add a line to the subplot I specified in R
Add a line to the subplot I specified in R

Time:09-08

I have already created 4 plots

x1 <- rnorm(10)
x2 <- rnorm(10)
x3 <- rnorm(10)
x4 <- rnorm(10)

par(mfrow=c(2,2))
 plot(x1,t="p",main = "1")
 plot(x2,t="p",main = "2")
 plot(x3,t="p",main = "3")
 plot(x4,t="p",main = "4")

enter image description here

I want to add a line to the first plot.. I can add a line at the initial stage of running the code

x1 <- rnorm(10)
x2 <- rnorm(10)
x3 <- rnorm(10)
x4 <- rnorm(10)

par(mfrow=c(2,2))
 plot(x1,t="p",main = "1") ; lines(rnorm(10),col=2)
 plot(x2,t="p",main = "2")
 plot(x3,t="p",main = "3")
 plot(x4,t="p",main = "4")

enter image description here

But the question is : how to add a line to the first plot after all four plots already exist, like in first picture

x1 <- rnorm(10)
x2 <- rnorm(10)
x3 <- rnorm(10)
x4 <- rnorm(10)

par(mfrow=c(2,2))
 plot(x1,t="p",main = "1") 
 plot(x2,t="p",main = "2")
 plot(x3,t="p",main = "3")
 plot(x4,t="p",main = "4")

 
 # how to add line in first plot now ?
 lines(rnorm(10),col=2)

CodePudding user response:

The graphical parameter mfg:

A numerical vector of the form c(i, j) where i and j indicate which figure in an array of figures is to be drawn next (if setting) or is being drawn (if enquiring). The array must already have been set by mfcol or mfrow.

par(mfrow = c(2, 2))
plot(x1, main = "1") 
plot(x2, main = "2")
plot(x3, main = "3")
plot(x4, main = "4")

par(mfg = c(1, 1))
lines(rnorm(10), col = 2)

enter image description here

  • Related