2013-03-30 171 views
6

我有是參數化三維曲線的數據:如何在R中平滑繪製參數化三維曲線?

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 
.............................. 

我發現scatterplot3dplot3d真的很酷。但我需要一個平滑的3d曲線。

我該如何繪製R?

+0

你有生成的'xyz'值t'的'參數化功能?但即使不是這樣,你也想在三維點之間插入你已經擁有的點 - 並且比簡單的線性插值做得更好? – Spacedman

+1

'scatterplot3d'有一個'type'參數,您可以設置爲'「l」'來鏈接您的點並形成曲線。 –

+0

@Spacedman是的,我想要比簡單線性插值更好的東西。 – midas

回答

10

您可以使用spline在點之間進行插值並平滑曲線。

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

按@ Spacedman的評論,你也可以使用rgl: 這允許您以交互方式旋轉現場。

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

+0

Bravo!如果可以的話,我會給予更多的信任。 –

+1

你有沒有試過用於3d地塊的rgl包? – Spacedman

+0

我想這是他們如何創建過山車! – PascalVKooten