6

交互式 3D 绘图的可能软件包之一是 rgl。我想做的是根据一些因素变量用颜色编码构建 3D 散点图。3D 维度散点图用于 plsr 分析产生的载荷。

结果图看起来像

根据变量组使用颜色编码的 PLS 载荷的 3D 散点图

示例数据在表格中给出:

> loadings

      |      Comp 1         |        Comp 2            |    Comp 3           | Class
-------------------------------------------------------------------------------------------                    
TEMP  | -0.0607044182964255 | "0.0437618450165671"     |"0.045124991801441"  | "global"  
MW    | "-0.13414890573833" |   "-0.0970537799069731"  |0.263043734662182"   | "local" 
DM    |"-0.183751529577861" |  "-0.102703237685933"    |"0.0640549385564205" | "global" 
CHG   |"-0.0558781715833019"| "0.125155347350922"      |"-0.119258450107321" | "local"

或者可以生成:

loadings <- data.frame(Comp1 = c(1.2, 3.4, 5.6, 13.1), Comp2 = c(4.3, 1.2, 7.7, 9.8),
                       Comp3 = c(1.2,6.9,15.6,15.0), 
                       row.names = c("TEMP", "MW", "DM", "CHG"), 
                       Class = c("global", "local", "global", "local"))
scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
          point.col = as.numeric(as.factor(loadings[,4])), size = 10)

获得的情节具有相同的风格,但更简单,因为只有两个级别的变量“类”:“全局”和“局部”

在此处输入图像描述

问题是:是否有可能在 rgl 中添加图例,或者可以将一些独立的图例附加到情节中?提前感谢您的帮助!

答案是:

scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
          point.col = as.numeric(as.factor(loadings[,4])), size = 10, type = 's')
text3d(x=1.1, y=c(.9,1), z=1.1,levels(loadings[[4]]),col="black")
points3d(x=1.2,y=c(.9,1),z=1.1, col=as.numeric(as.factor(loadings[,4])), size=5)

根据类别带有标签的图: 在此处输入图像描述

4

2 回答 2

1

那不是plot3d图像(除非您可能加载了另一个包),但看起来像是由 John Fox 使用包中的函数scatter3d构造的渲染:scatter3dcar

  require(rgl)
  require(car)
  scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
            point.col = as.numeric(as.factor(loadings[,4])), size = 10)

scatter3d函数确实依赖于 rgl 函数,但有很多自定义选项。您没有提供构建“图例”的代码,所以我使用了 rgl::text3d 中提供的示例:

 text3d(1+ font/8, 1+cex/4, 1+famnum/8, text=paste(family, font), adj = 0.5, 
       color="blue", family=family, font=font, cex=cex)

在此处输入图像描述

使用新数据,这是对文本和点的请求的响应:

 scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
             point.col = as.numeric(as.factor(loadings[,4])), size = 10)
 text3d(x=1.1, y=c(.9,1,1.1), z=1.1, names(loadings) ,col="black")
 points3d(x=1.2,y=c(.9,1,1.1),z=1.1, col=as.numeric(as.factor(loadings[,4])), size=5)

在此处输入图像描述

于 2013-09-27T18:15:17.553 回答
1

我使用公式方法创建组,并通过将组标签变为白色来“删除”组标签,因为我无法弄清楚如何以更简单的方式做到这一点。然后我使用 ... 创建了一个图例legend3d,这非常简单。只需确保点和图例颜色匹配即可。也许这很笨重,但它完成了我需要它做的事情!

scatter3d(Biomass~Salinity*Moisture|Competitor, data=phrag19, labels=FALSE,
    surface=FALSE,
      grid = FALSE, 
        ellipsoid = FALSE,
    axis.col = c("black", "black", "black"),
    text.col=c("white","white","white","white","white","white","white"),
    surface.col=c("#1D1B1A","#C70039", 
"#94CC00","#A5D7F3","#D2A7DC","#1CAD7A","#FFB114"))

legend3d("right", legend = levels(phrag19$Competitor),
   col =  c("#1D1B1A","#C70039", "#94CC00","#A5D7F3","#D2A7DC","#1CAD7A","#FFB114"), 
pch = 18)

传奇3d

于 2021-03-16T17:57:36.580 回答