Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a line to the subplot I specified in R

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading