As explained the graphics manual, only plots from high-level plotting commands (e.g. plot.new()
) and complete expressions are recorded. That means if you have multiple low-level plot changes in a for-loop, these changes will not be recorded one by one, because the for-loop in only one R expression. That is what Figure 4 in the manual illustrates.
If you want to create an animation from a for-loop, there must be high-level plotting commands in the loop. Figure 7 in the manual is such an example.
In your case, you have to move the plot()
call into the loop:
x = 1:2
for (i in 1:length(x)) {
plot(x, x, type = "n")
points(x[1:i], x[1:i])
}
Yes, this looks like a serious waste of resource, and the "natural" way should be adding points one by one as you did, instead of opening a new plot and drawing points from 1
to i
, but there is no way to detect low-level graphical changes inside a single R expression.