-1

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?

rappr
  • 135
  • 7
  • 4
    Did you search before asking this question? –  May 15 '15 at 07:34
  • I found animation packages, external software, but nothing as simple as in my question. – rappr May 15 '15 at 07:39
  • 1
    You may want something like this: http://en.wikipedia.org/wiki/R_%28programming_language%29#Example_2 – user1436187 May 15 '15 at 08:22
  • Thank you user1436187, I have just tried it - on my system it actually produced a 4MB gif image that unfortunately shows a static blurred image… I find R a bit frustrating, it seems that every instruction that I am used to write in Matlab easily is either impossible or gets disproportionately complex in R. – rappr May 15 '15 at 08:46

1 Answers1

1

I'm not about to try to figure out what all the matlab plotting is doing w/r/t aesthetics.

This:

while(TRUE) {
  barplot(sample(1:1000, 10))
  Sys.sleep(0.5)
}

"animates" a random bar chart display pretty well IMO and:

t <- (1:360) / 180 * pi
n <- length(t)
x <- cos(t)
y <- sin(t)

for (i in 1:n) {
  plot(x[1:i], y[1:i], type="p")
  Sys.sleep(0.15)
}

does one basic bit of "animation" of the points in one fashion and:

for (i in 1:n) {
  plot.new()
  points(x[1:i], y[1:i])
  Sys.sleep(0.15)
}

in another (though that one needs some work setting x & y limits to avoid graphics device errors).

All three stop when asked to "stop" by the GUI or when the for loop is done.

Not exactly "disproportionately complex". More like "not familiar to the matlab person".

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205