6

I have the data which are parametric 3d curve:

    t        x       y       z
0.000    3.734   2.518  -0.134    
0.507    2.604   9.059   0.919
0.861    1.532  11.584  -0.248
1.314    1.015   1.886  -0.325
1.684    2.815   4.596   3.275
1.938    1.359   8.015   2.873
2.391    1.359   8.015   2.873
..............................

I found scatterplot3d and plot3d really cool. But I need a smooth 3d curve.

How can I plot it in R?

midas
  • 1,758
  • 2
  • 20
  • 20
  • Do you have the parametric function of `t` that generated the `xyz` values? But even if not, you want to interpolate in 3d between the points you already have - and do better than a simple linear interpolation? – Spacedman Mar 30 '13 at 10:37
  • 1
    `scatterplot3d` has a `type` argument, which you can set to `"l"` to link your points and form a curve. – Vincent Zoonekynd Mar 30 '13 at 10:40
  • @Spacedman Yes, I want something better than simple linear interpolation. – midas Mar 30 '13 at 11:00

1 Answers1

10

You can use spline to interpolate between your points and smooth the curve.

d <- read.delim(textConnection(
"t x y z
0.000 3.734 2.518 -0.134
0.507 2.604 9.059 0.919
0.861 1.532 11.584 -0.248
1.314 1.015 1.886 -0.325
1.684 2.815 4.596 3.275
1.938 1.359 8.015 2.873
2.391 1.359 8.015 2.873"
), sep=" ")
ts <- seq( from = min(d$t), max(d$t), length=100 )
d2 <- apply( d[,-1], 2, function(u) spline( d$t, u, xout = ts )$y ) 
library(scatterplot3d)
p <- scatterplot3d(d2, type="l", lwd=3)
p$points3d( d[,-1], type="h" )

Smoothed curve

As per @Spacedman's comment, you could also use rgl: this allows you to interactively rotate the scene.

library(rgl)
plot3d( d2, type="l", lwd=5, col="navy" )
points3d(d[,-1])
spheres3d(d[,-1], radius=.1, col="orange")
segments3d( matrix( t( cbind( d[,-1], d[,2:3], 0 ) ), nc=3, byrow=TRUE ) )
planes3d(0,0,1,0, col="yellow", alpha=.5)  # Plane z=0

enter image description here

Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78