gpt4 book ai didi

r - 向 scatter3d 图添加图例

转载 作者:行者123 更新时间:2023-12-04 15:58:48 25 4
gpt4 key购买 nike

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

结果图看起来像

3D scatterplot of PLS loadings with color coding according to the groups of variables

示例数据 在表中给出:

> 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)

获得的情节具有相同的风格,但更简单,因为变量“Class”只有两个级别:'global'和'local'

enter image description here

问题是 :
是否有可能在 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)

根据类别带有标签的图:
enter image description here

最佳答案

那不是 plot3d图像(除非您可能加载了另一个包),但看起来像 scatter3d使用 scatter3d 构建的渲染来自 car 的函数约翰福克斯的包裹:

  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 function 确实依赖于 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)

enter image description here

使用新数据,这是为了响应对文本和点的请求:
 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)

enter image description here

关于r - 向 scatter3d 图添加图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19045208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com