问题描述
我的数据看起来像下面这样:
My data looks like following:
> 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 =间距(在距离差)(在主车辆与前车之间的速度差)如下:
The complete plot of x= deltaV (difference in speeds between subject vehicle and leading vehicle) and y=Spacing (difference in distance) is shown below:
我想用动画
包,这样一个数据点在时间显示和整个情节是在动画的结尾画动画吧。我不知道这是否是这样做正确的软件包。
I want to animate it using animation
package such that one data point is shown at a time and whole plot is drawn at the end of animation. I am not sure if this is the right package to do this. I tried following with no output (not even a static plot) without an error:
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)
是所需类型的动画可以使用这个包?
Is the required kind of animation possible using this package?
推荐答案
这应该工作:
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()
我可以解释如何看待这一设置,但我认为博客文章做了
好工作,应该给你的感觉。你可以撕code拆开来看看我是如何做的。
I could explain how to think about setting this up but I think THIS blog post on this does agood job and should give you a feel. You can tear the code apart to check out how I did it.
type <- if(.Platform$OS.type == "windows") shell else system
saveGIF(FUN(), interval = 0.1, outdir = getwd(), cmd.fun = type)
这篇关于动画中的R的每个数据点(行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!