I am new to R. I have 4 graphs on my page [par(mfrow =c(4,1)] and put one plot on each. How can I now select one of these graphs and add another plot on it while retaining the original plot?
Asked
Active
Viewed 33 times
2
-
3Welcome to stackoverflow. Could you provide some more information? It will increase your chances of obtaining some help. I would suggest [this link](https://stackoverflow.com/help/minimal-reproducible-example) to help you format your question. – DJJ Mar 27 '20 at 13:12
-
I don't know if it is possible but as a quick hack you can at least everything again. consider this `x<-rnorm(10); a <- cbind(1+1.5*x,x);par(mfrow =c(4,1));plot(a);abline(lm(a[,1]~a[,2]),col="blue");`. So you can add line and points to the same graph as long as you don't plot a new one. – DJJ Mar 27 '20 at 13:19
-
thanks very much but ideally I would like to return to a graph after plotting a new one – David Mar 27 '20 at 13:52
2 Answers
1
You can use par(mgp = plot_coords)
, as stated in this post. e.g :
par(mfrow = c(2,2))
plot(1:10, col = 1, main = 1)
plot(1:10, col = 2, main = 2)
plot(1:10, col = 3, main = 3)
plot(1:10, col = 4, main = 4)
par(mfg = c(1,2))
points(10:1, col = "hotpink")

RoB
- 1,833
- 11
- 23
0
RoB is correct, but this only works if all the graphs are the same scale. If plots have different scales, you will need to reset the window scaling to the appropriate values for the panel before adding a new element.
To demonstrate with a modified version of RoB's example, this code does not generate the expected plots.
par(mfrow = c(2,2))
plot(1:12, col = 1, main = 1)
plot(1:10, col = 2, main = 2)
plot(1:12, col = 3, main = 3)
plot(1:12, col = 4, main = 4)
par(mfg = c(1,2))
points(10:1, col = "hotpink")
This code does generate the expected plots.
par(mfrow = c(2,2))
plot(1:12, col = 1, main = 1)
plot(1:10, col = 2, main = 2)
plot(1:12, col = 3, main = 3)
plot(1:12, col = 4, main = 4)
par(mfg = c(1,2))
plot.window(xlim = c(1, 10), ylim = c(1, 10))
points(10:1, col = "hotpink")

Robert Payn
- 61
- 3