我正在使用动画包来绘制参数曲线
$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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)