r动画,参数曲线

标签 r

我正在使用动画包来绘制参数曲线 $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/

相关文章:

回程数据的 R 组列

从长到宽 reshape 数据框

r - 将 on.exit expr 添加到父调用?

r - 使用 geom_tile 可视化表格?

r - 如何使用ggplot2反转堆积面积图中的堆积顺序

r - 确定值更改的日期并使用 R 中的 sum() 和 diff() 汇总数据

r - R : cannot open file 'specdata/001.csv' : No such file or directory [duplicate]

r - dplyr 。和 _no 全局变量 '.' 的可见绑定(bind) _ 包检查中的注意事项

html - 从 Google Images 在 R 中抓取网页

r - dyld : Library not loaded:/usr/local/opt/openblas/lib/libopenblasp-r0. 2.20.dylib