This is a follow-up of this question.
I wanted to plot multiple curves on the same graph but so that my new curves respect the same y-axis scale generated by the first curve.
Notice the following example:
y1 <- c(100, 200, 300, 400, 500)
y2 <- c(1, 2, 3, 4, 5)
x <- c(1, 2, 3, 4, 5)
# first plot
plot(x, y1)
# second plot
par(new = TRUE)
plot(x, y2, axes = FALSE, xlab = "", ylab = "")
That actually plots both sets of values on the same coordinates of the graph (because I’m hiding the new y-axis that would be created with the second plot).
My question then is how to maintain the same y-axis scale when plotting the second graph.
(The typical method would be to use
plotjust once to set up the limits, possibly to include the range of all series combined, and then to usepointsandlinesto add the separate series.) To useplotmultiple times withpar(new=TRUE)you need to make sure that your first plot has a properylimto accept the all series (and in another situation, you may need to also use the same strategy for xlim):This next code will do the task more compactly, by default you get numbers as points but the second one gives you typical R-type-“points”: