Plot and animate (x,y,z) position data of an object over time (t) in R

3k views Asked by At

I'm trying to plot the position of an object in 3D space (x, y, and z co-ordinates) over time so that I can determine the path of the object.

I'm finding it hard to visualise how this would work. I can plot the (x,y,z) coordinates in a 3D space, but how to visualize the progress of time?

I am using R, and I have attached below the results of scatterplot3d() function in R.

Could you please guide me how to visualize the dataset. Also, it would help me if you could suggest any other tools for this type of visualization.

[image] https://www.dropbox.com/s/6douprzlllfhd3c/Screenshot%202014-11-10%2019.14.19.png?dl=0

[sample data set]

Time    X   Y   Z

1   245.00  198.00  247.00
2   247.00  197.50  246.50
3   249.00  198.20  245.20
5   250.25  200.50  243.75
6   249.20  202.80  242.40
7   251.00  201.75  241.50
8   249.40  199.20  241.80
9   252.80  196.20  242.20
10  256.60  200.80  242.20
2

There are 2 answers

3
keegan On BEST ANSWER

For an interactive, browser-based animation, check out the animation package:

library(animation)
df<-matrix(rnorm(90),ncol=3)
saveHTML({
    for (i in 1:nrow(df)) {
    pt<-df[i,]
    scatterplot3d(pt[1],pt[2],pt[3],
    xlim=c(-4,4),ylim=c(-4,4),zlim=c(-4,4)
      )}
})

EDIT: include library call


Or this, which uses OP's data and plots the evolving path.

library(animation)
library(scatterplot3d)
saveHTML({
  for (i in 2:nrow(df)) {
    with(df[1:i,],scatterplot3d(X,Y,Z,type="l",
         xlim=range(df$X),ylim=range(df$Y),zlim=range(df$Z)))
    }
})
0
jlhoward On

Expanding on the comment (calling your sample df).

library(rgl)
with(df,lines3d(X,Y,Z))
with(df[1,],points3d(X,Y,Z,size=7,col="red"))
with(df[-1,],points3d(X,Y,Z,col="blue"))
axes3d()
title3d(xlab="X",ylab="Y",zlab="Z")

This code produces a rotatable 3D plot of your data. Below is a screen shot. The red dot is the starting point.