简体   繁体   中英

3D scatter plot in real time

I am working in R and have data of the following form. 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. 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.

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. 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 . The latter requires installation of 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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