我的数据如下:
> dput(head(tht[,c(2,3,18,28)],15))
structure(list(Vehicle.ID = c(32L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L), Frame.ID = 184:198,
Spacing = c(65.76, 65.26, 64.76, 64.26, 63.75, 63.23, 62.72,
62.3, 61.99, 61.72, 61.4, 61.02, 60.68, 60.42, 60.2), deltaV = c(3.13,
3.2, 3.26, 3.29, 3.3, 3.3, 3.27, 3.23, 3.18, 3.13, 3.07,
3.02, 2.96, 2.89, 2.83000000000001)), .Names = c("Vehicle.ID",
"Frame.ID", "Spacing", "deltaV"), row.names = 3515:3529, class = "data.frame")
x = deltaV(主车辆与主要车辆之间的速度差异)和y =间距(距离差异)的完整图表如下所示:
我想使用animation
包为其设置动画,以便一次显示一个数据点,并在动画结束时绘制整个绘图。我不确定这是否是正确的包。我试着跟随没有输出(甚至没有静态图)没有错误:
library(animation)
library(ggplot2)
## set some options first
oopt <- ani.options(interval = 0.1, nmax = nrow(tht))
## use a loop to create images one by one
for (i in 1:ani.options("nmax")) {
qplot(deltaV, Spacing,data=tht)
ani.pause() ## pause for a while (’interval’)
}
## restore the options
ani.options(oopt)
使用此包可以获得所需的动画吗?
答案 0 :(得分:8)
这应该有效:
library(animation)
library(ggplot2)
## set some options first
oopt <- ani.options(interval = 0.1)
FUN <- function() {
lapply(1:nrow(tht), function(i) {
print(ggplot(tht[1:i,, drop=FALSE], aes(y = Spacing, x = deltaV)) +
geom_point() +
xlim(c(min(tht$deltaV), max(tht$deltaV))) +
ylim(c(min(tht$Spacing), max(tht$Spacing))))
animation::ani.pause()
})
}
FUN()
我可以解释如何考虑如何设置它,但我认为THIS关于此的博客文章确实如此 干得好,应该给你一个感觉。您可以拆开代码以查看我是如何做到的。
type <- if(.Platform$OS.type == "windows") shell else system
saveGIF(FUN(), interval = 0.1, outdir = getwd(), cmd.fun = type)