这可以使用 R 包 scatterplot3d 和 animation 来完成。后者需要安装ImageMagick。
这里有一些代码可以完成你想做的事情。
require(animation)
require(scatterplot3d)
# Get some example data
n <- 10
dt <- data.frame(birth = rnorm(n, 50, 20),
age = sample(1:100, n, replace=TRUE),
cause = sample(1:3, n, replace=TRUE),
race = sample(1:5, n, replace=TRUE),
bweight = rnorm(n, 1000, 200),
blen = rnorm(n, 300, 20))
# Starting and final timepoint
st <- 1
ft <- 150
# All the timepoints to evaluate
times <- st:ft
# Matrices that show for each timepoint whether a person is alive or dead.
born <- outer(dt$birth, times, "<")
dead <- outer(dt$birth + dt$age, times, "<")
# Matrix is 0 for not yet born, 1 for living, 2 for dead.
status <- born + dead
# If dead, this contains the status of the cause.
deadcause <- matrix(dt$cause, nc=length(times), nrow=n) * dead + born
# Function to get animations
anim <- function(dt, times) {
for(i in seq_along(times)) {
# Remove those not yet born.
dtcur <- dt * born[, i]
scatterplot3d(x=dtcur$race,
y=dtcur$bweight,
z=dtcur$blen,
color=deadcause[, i],
angle = 135, type = "h",
main=paste("At time", i),
xlim=c(0,5),
ylim=c(0,1500),
zlim=c(0,500))
animation::ani.pause()
}
}
# Save the gif to current directory.
saveGIF(expr = anim(dt, times), interval=.1, outdir = getwd())