Is it possible to create an animation in R directly, as in Matlab, without actually saving figures and using external software? Just executing a script?
Consider the following example in Matlab.
t = (1:360) / 180 * pi; % 360 angles in radians from 1 to 360 degrees
n = length(t); % Length of vector
x = cos(t); % Cosines
y = sin(t); % Sines
f = figure; % Create figure and its handle
hh = plot(2, 2, 'or', 'MarkerFaceColor', 'r', 'MarkerSize', 10); % Create plot and its handle
set(gca, 'XLim', [-1 1]); % Set x axis limits
set(gca, 'YLim', [-1 1]); % Set y axis limits
axis square; axis off; % Set more properties for axes
while ishandle(f) % Until the user closes the figure
try % Try this loop, if encouter an error just break the loop
for ii = 1:n % For all angles
set(hh, 'XData', x(ii)) % Change point x coordinate
set(hh, 'YData', y(ii)) % Change point y coordinate
pause(0.01) % Make a little pause
end
end
end
After hrbrmstr's answer, I have tried this:
t <- (1:360) / 180 * pi
n <- length(t)
x <- cos(t)
y <- sin(t)
while(TRUE) {
for (i in 1:n) {
plot(x[i], y[i], ann=FALSE, pch=20, axes=FALSE, xlim=c(-1, 1), ylim=c(-1, 1), col="red", cex=4, asp=1)
Sys.sleep(0.01)
}
}
and it seems to be doing the job. Thanks!
I have also tried this:
t <- (1:360) / 180 * pi
n <- length(t)
x <- cos(t)
y <- sin(t)
while(TRUE) {
for (i in 1:n) {
plot.new()
usr<-par("usr")
par(usr=c(-1.1, 1.1, -1.1, 1.1))
lines(x, y, col="green")
points(x[i], y[i], pch=20, col="red", cex=4, asp=1)
Sys.sleep(0.01)
}
}
which is closer to what I had initially in mind. However I find R's "paper and pen" drawing model just terrible. Isn't there a way around that?