R base graphics. Specify which graph I am plotting to

Viewed 30

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?

2 Answers

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")

enter image description here

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")
Related