## Loading required package: ggplot2
#跑了這動畫跑非常久,不得以參考KY同學的將張數下修
Brownian <- function(n = 11, pause = 0.05, nIter = 64, ...) {
x = rnorm(n)
y = rnorm(n)
i = 1
while (i <= nIter) {
plot(x, y, ...)
text(x, y, cex = 0.5)
x = x + rnorm(n)
y = y + rnorm(n)
Sys.sleep(pause)
i = i + 1
}
}
saveGIF(Brownian(xlim = c(-20, 20), ylim = c(-20, 20),pch = 21, cex = 2, col = "cyan", bg = "lavender"))## Output at: animation.gif
## [1] TRUE
test it
/Users/Tjlee/Desktop/weather/animation.gif
##此作法參考KY跟Chang, C. Y.兩位同學再進行微調
Brownian2 <- function(n = 11, pause = 0.05,nIter = 8, ...) {
x = rnorm(n)
y = rnorm(n)
i = 1
repeat {
if(i > nIter) break # 重複做,一旦大於8就停止
plot(x, y, ...)
text(x, y, cex = 0.5)
x = x + rnorm(n)
y = y + rnorm(n)
Sys.sleep(pause)
i = i + 1
}
}
Brownian2(xlim = c(-20, 20), ylim = c(-20, 20),pch = 21, cex = 2, col = "cyan", bg = "lavender")