将点和线添加到 R 中的 3D 散点图
Adding point and lines to 3D scatter plot in R
我想在 3d 散点图中可视化关于主要成分的浓度椭圆体(主要成分作为这些椭圆体的轴)。我使用带有选项 ellipsoid = TRUE
的函数 scatter3d
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)
绘制此图:
然后我尝试使用
添加"mean point"
points3d(mean(data3d[,1]), mean(data3d[,2]), mean(data3d[,3]), col="red", size=20)
但是这个点不在它应该在的地方(在椭圆体的中心):
我想知道为什么以及如何重新缩放它 (?)。还有一个问题,这之后会出现如何将这个椭圆体的轴添加到绘图中?
查看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)
我想在 3d 散点图中可视化关于主要成分的浓度椭圆体(主要成分作为这些椭圆体的轴)。我使用带有选项 ellipsoid = TRUE
的函数 scatter3ddata3d <- 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)
绘制此图:
然后我尝试使用
添加"mean point"points3d(mean(data3d[,1]), mean(data3d[,2]), mean(data3d[,3]), col="red", size=20)
但是这个点不在它应该在的地方(在椭圆体的中心):
我想知道为什么以及如何重新缩放它 (?)。还有一个问题,这之后会出现如何将这个椭圆体的轴添加到绘图中?
查看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)