将点和线添加到R中的3D散点图

时间:2015-05-03 13:07:02

标签: r pca

我想在三维散点图中显示关于主成分(主要成分作为这些椭球的轴)的浓度椭球。我使用函数scatter3d和选项ellipsoid = TRUE

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 个答案:

答案 0 :(得分: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)