繁体   English   中英

实时3D散点图

[英]3D scatter plot in real time

我正在R中工作,并具有以下形式的数据。 第一列是一个人的生日,第二列是年龄,第三列是死亡原因(自然/疾病/事故),第四列是种族,第五列是出生时的体重,第六列是出生时的长度。

最终,我想创建一个简短的视频/时间流逝的3D图,最后三列为空间尺寸。 视频播放时间对应于收集我的数据的总时间。 视频播放时,点会以黑点的形式弹出到其适当的3D位置,然后根据其死因变成蓝色/绿色/红色。

在追求这个想法时,我遇到了这个问题 ,但是它似乎是在实时地移动轴,而不是实时地填充图形。 我担心这从根本上是不同的任务。 我还看到jfreechart.com对此很有用,但我更喜欢在R / Matlab中在可能的情况下使用其他软件来完成此操作。 最后,我愿意使用/学习实现此目的所需的任何软件。

感谢您抽出宝贵的时间!

这可以使用R包scatterplot3danimation来完成。 后者需要安装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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM