2015-05-03 160 views
4

我想要在三維散點圖中關於主要成分(主要成分作爲這些橢圓的軸)可視化濃度橢球。我用選項橢圓函數scatter3d = TRUE將點和線添加到R中的三維散點圖

data3d <- iris[which(iris$Species == "versicolor"), ] 

library(car) 
library(rgl) 
scatter3d(x = data3d[,1], y = data3d[,2], z = data3d[,3], 
     surface=FALSE, grid = TRUE, ellipsoid = TRUE, 
     axis.col = c("black", "black", "black"), axis.scales = FALSE, 
     xlab = "X1", ylab = "X2", zlab = "X3", surface.col = "blue", 
     revolution=0, ellipsoid.alpha = 0.0, level=0.7, point.col = "yellow", add=TRUE) 

作出這樣的情節:

enter image description here

然後我試圖加入 「中間點」 使用

points3d(mean(data3d[,1]), mean(data3d[,2]), mean(data3d[,3]), col="red", size=20) 

但這點不在它應該在的位置(在橢球體的中心): enter image description here

我想知道爲什麼以及如何重新調整它(?)。還有另一個問題,在這之後會出現如何將這個橢球的座標軸添加到圖中?

+1

'par3d()'表示該框已縮放爲單位立方體? –

回答

5

看着car:::scatter3d.default顯示座標內部縮放每個維度的最小值和最大值;以下代碼在繪圖之前進行縮放:

sc <- function(x,orig) { 
    d <- diff(range(orig)) 
    m <- min(orig) 
    (x-m)/d 
} 
msc <- function(x) { 
    sc(mean(x),x) 
} 

points3d(msc(data3d[,1]), 
     msc(data3d[,2]), 
     msc(data3d[,3]), col="red", size=20)