[R] Animation and a better Quincunx

Kjetil Halvorsen khal at alumni.uv.es
Mon Nov 30 10:00:04 CET 1998


The solution I sent yesterday, only changing the original
Quincunx.prg() adding the magic cat(""), isn't very satisfactory.

This is a complicated animation, using two panels, and (at least
on rw063) drawing in only one panel destroys the content of the
other. A better possibility seems to use (the new) function
split.screen, see Quincunx.screen appended at tail. 

That led into some other problems as well:

Quincunx.screen eats memory, with a standard memory
configuration runs out of heap at around trial 50. There
is no intrinsic reason why the memory needs of an animation
should depend on the number of frames seen?

after choosing screen s with screen(s), using a high level
plot function once doesnt destroy the other screens, but the
second times it does. A Bug?

There is some overlap between screens. Look at he ylab of the
second screen, "Frecuency", under animation!



 Quincunx.screen  <-  
function(trials = 100)
{
  # Init: 
  #par(mfrow = c(1, 2))
 split.screen(c(1,2), erase=FALSE) # split.screen isn't in RJune
 xpoints <- seq(1, 25, 1)
 ypoints <- seq(1, 25, 1)
 xheights <- rep(0, 25)
 mcol <- 2
 xy <- expand.grid(xpoints, ypoints)
 xx <- seq(1, 25, 2)
 yy <- rep(0, 25)
 ypos <- ypoints + 0.4
 HT <- c("H", "T")

 # First loop:
 for(j in 1:trials) {
  screen(1)
  # Moved the drawing of the background into the main call. To have it
separate
  # makes the animation to discontinuous. 
  plot(x=xy[, 1], y= xy[, 2], type = "p", xlab = "", ylab = "", axes =
F, main =
       "Galton's Quincunx", pch=5, col="brown", cex=0.25) # Plotting in
the first panel.
  cat("")
  #points(x = xy[, 1], y = xy[, 2], pch = 5, col = "brown", cex=0.3)
  #cat("")
  marble.path <- sample(HT, 25, replace = T)
  xpos <- 13
  points(xpos, ypos[25], pch = 16, col = "red")
  cat("")
  for(i in 1:24) {
   if(marble.path[i] == "H")
    xpos <- xpos + 1
   else xpos <- xpos - 1
   if(xpos > 25)
    xpos <- 25
   if(xpos < 0)
    xpos <- 0
   points(xpos, ypos[25 - i], pch = 16, col = "red", cex=0.7)
   cat("")
  } # end inner loop
  xheights[xpos] <- xheights[xpos] + 1
  text(x = xpoints[xx], y = yy[xx], labels = as.character(xheights)[xx])
  cat("")
  screen(2)
  plot(xpoints[xx], xheights[xx], ylim = c(0, trials/2), type = "h",
main =
       paste("Trials = ", j, sep = ""), ylab = "Frequency",
   xlab = "X") # plotting in second panel.
  cat("")
  cat("")
 } # end outer loop
 close.screen()
}


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list