交互式3D绘图的一个可能包是rgl。我想要做的是根据某个因子变量构建带有颜色编码的3D散点图。三维散点图用于从plsr分析得到的载荷。
结果情节看起来像
示例数据在表格中给出:
> 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)
根据类别使用标签的情节:
答案 0 :(得分:1)
这不是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
函数确实依赖于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)
答案 1 :(得分: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)