简体   繁体   English

实时3D散点图

[英]3D scatter plot in real time

I am working in R and have data of the following form. 我正在R中工作,并具有以下形式的数据。 A data frame where the first column is a person's birth date, second is age in years, third is cause of death (natural/disease/accident), fourth is race, fifth is weight at birth, and the sixth is length at birth. 第一列是一个人的生日,第二列是年龄,第三列是死亡原因(自然/疾病/事故),第四列是种族,第五列是出生时的体重,第六列是出生时的长度。

Ultimately, I want to create a short video/time lapse of a 3D graph with the last three columns as spatial dimensions. 最终,我想创建一个简短的视频/时间流逝的3D图,最后三列为空间尺寸。 The video playtime corresponds to the total time over which my data is collected. 视频播放时间对应于收集我的数据的总时间。 As the video plays, points pop into their appropriate 3D position as black dots, and later, turn blue/green/red depending on their cause of death. 视频播放时,点会以黑点的形式弹出到其适当的3D位置,然后根据其死因变成蓝色/绿色/红色。

In pursuing this idea, I came across this , but it seems to shift the axis in real time and not populate the graph in real time. 在追求这个想法时,我遇到了这个问题 ,但是它似乎是在实时地移动轴,而不是实时地填充图形。 I fear this is fundamentally a different task. 我担心这从根本上是不同的任务。 I also saw jfreechart.com is useful for this, but my preference is to accomplish this in R/Matlab if its possible before resorting to other software. 我还看到jfreechart.com对此很有用,但我更喜欢在R / Matlab中在可能的情况下使用其他软件来完成此操作。 In the end, I am open to using/learning whatever software necessary to accomplish this. 最后,我愿意使用/学习实现此目的所需的任何软件。

Thanks for taking the time! 感谢您抽出宝贵的时间!

This can be accomplished using the R packages scatterplot3d and animation . 这可以使用R包scatterplot3danimation来完成。 The latter requires installation of ImageMagick . 后者需要安装ImageMagick

Here is some code that accomplishes something like what you want to do. 这是一些代码,可以完成您想要执行的操作。

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