我正在使用动画包来绘制参数曲线 $x=sin(t)$ 和 $y=sin(t)^2$ 以及使用以下代码追踪曲线的圆:
require(animation)
x <- seq(-1,1,length=20)
y <- x^2
plot(x,y,type="l")
library(animation)
ani.record(reset=TRUE)
t <- seq(0,4*pi,by=pi/30)
for (i in 1:length(t)) {
points(sin(t[i]),sin(t[i])^2,pch=19,cex=2)
ani.record()
plot(x,y,type="l") # I have a question in this line
}
ani.replay()
它可以工作(请复制并粘贴此代码),但录制需要相当长的时间。这是一条简单的曲线,但我应该想要吗 做复杂的,会花费太多时间来完成动画录制。 我可以改进的一件事是不使用第二个绘图函数重新绘制抛物线以消除上一个图中出现的圆圈。 有什么可以改进的吗?
最佳答案
基础 R 包 grDevices
有两个函数可以保存和回放绘图,专为以下情况而设计:
recordPlot()
replayPlot()
在我的机器上进行的一些主观测试似乎表明它比重新绘制整个图要快:
require(animation)
x <- seq(-1,1,length=20)
y <- x^2
plot(x,y,type="l")
oopts <- ani.opts(interval=0.25)
p <- recordPlot() # <== Record plot here =============
ani.record(reset=TRUE)
t <- seq(0,4*pi,by=pi/30)
for (i in 1:length(t)) {
replayPlot(p) # <== Replay plot here =============
points(sin(t[i]),sin(t[i])^2,pch=19,cex=2)
}
ani.replay()
关于r动画,参数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11919390/