如何将点添加到scatter3d?

时间:2019-06-12 14:22:03

标签: r rgl

我正在尝试使用R的交互式“ rgl”包在同一3D散点图中绘制两组点

这是数据:

> head(tsne_train)
           X1         X2          X3
2   18.940912 -46.761145 -56.1818708
4   17.768953  68.678871   0.8070582
6   -2.440751 -53.021051  55.0437596
7   44.740812  -2.347877 -54.1501468
8  -87.924687  15.354890 -30.1806330
12  21.991465  38.406572 -33.0551010`

> head(tsne_test)
          X1         X2          X3
1   1.585156  71.568255    7.438958
3  62.204021  -3.817038  -37.609328
5   5.422276 -21.855152   66.865478
9  21.223133 -29.763255 -107.832779
10  9.037427 -62.816717   65.560664
11 24.775047 -51.820532   57.106795

这是我到目前为止尝试过的:

scatter3d( x=tsne_train[,1],y=tsne_train[,2],z=tsne_train[,3],
          groups = train$Result,
          surface=F, grid = T, ellipsoid = FALSE,sphere.size = 1.5,
          surface.col = c("red", "orange", "green"),fogtype="none",
          axis.col=c("black","black","black"))

产生:    enter image description here

scatter3d(x=tsne_test[,1],y=tsne_test[,2],z=tsne_test[,3], groups = test$Result,
                surface=F, grid = T, ellipsoid = FALSE,sphere.size = 1.5,
                col = c("purple", "blue", "cyan"), fogtype="none",
                axis.col=c("black","black","black"))

产生:    enter image description here

我想要一个散点图中的所有点,但保留颜色分组(总共6种颜色)。有点像在图1上覆盖图2

使用spheres3d向图中添加更多点会导致混乱:

spheres3d(x=tsne_test[,1],y=tsne_test[,2],z=tsne_test[,3],
         surface=F, grid = T,
         col = c("purple", "blue", "cyan"), sphere.size = 3,
         axis.col=c("black","black","black"))

enter image description here

这不会保留图1中的绿色/红色/橙色点,轴不可见,并且点的位置看起来很隐蔽,因为图2看起来并不像它。也许我只是在使用spheres3d错误?

1 个答案:

答案 0 :(得分:0)

这是根据我的建议编写的一些代码。我们没有您的真实数据,因此我使用了您提供的数据,并为组提供了随机值。

tsne_train <- read.table(textConnection(
"           X1         X2          X3
2   18.940912 -46.761145 -56.1818708
4   17.768953  68.678871   0.8070582
6   -2.440751 -53.021051  55.0437596
7   44.740812  -2.347877 -54.1501468
8  -87.924687  15.354890 -30.1806330
12  21.991465  38.406572 -33.0551010"
), header=TRUE)
trainGroup <- factor(sample(1:3, 6, replace=TRUE))

tsne_test <- read.table(textConnection(
"          X1         X2          X3
1   1.585156  71.568255    7.438958
3  62.204021  -3.817038  -37.609328
5   5.422276 -21.855152   66.865478
9  21.223133 -29.763255 -107.832779
10  9.037427 -62.816717   65.560664
11 24.775047 -51.820532   57.106795"
), header=TRUE)
testGroup <- factor(sample(1:3, 6, replace=TRUE))

scatter3d(x = tsne_train[,1],
          y = tsne_train[,2],
          z = tsne_train[,3],
          groups = trainGroup,
          surface=FALSE, 
          grid = TRUE, 
          ellipsoid = FALSE,
          sphere.size = 1.5,
          surface.col = c("red",  "orange", "green"),
          fogtype = "none",
          axis.col = c("black","black","black"))
spheres3d(x = (tsne_test[,1]-min(tsne_train[,1]))/
              (max(tsne_train[,1]) - min(tsne_train[,1])),
          y = (tsne_test[,2]-min(tsne_train[,2]))/
              (max(tsne_train[,2]) - min(tsne_train[,2])),
          z = (tsne_test[,3]-min(tsne_train[,3]))/
              (max(tsne_train[,3]) - min(tsne_train[,3])),
          col = c("purple", "blue", "cyan")[testGroup], 
          radius = 0.1)

这是情节的样子:

screenshot

如果您不使用scatter3d函数,而使用rgl函数,那么这一切将变得更加容易。例如,

plot3d( x = tsne_train,
            col = c("red", "orange", "green")\[trainGroup\], 
            type="s")
spheres3d(x = tsne_test,
          col = c("purple", "blue", "cyan")\[testGroup\], 
          radius = 3)

这将产生以下情节:

screenshot