[英]Adding a legend to scatter3d plot
One of the possible packages for interactive 3D plots is rgl.交互式 3D 绘图的可能软件包之一是 rgl。 What I would like to do is to build 3D scatterplot with color coding according to some factor variable.
我想做的是根据一些因子变量构建带有颜色编码的 3D 散点图。 The 3D dimensional scatterplot is for resulting loadings from plsr analysis.
3D 维散点图用于 plsr 分析的结果载荷。
The resulting plot looks like结果图看起来像
The example data is given in a table:示例数据在表中给出:
> 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"
or can be generated:或者可以生成:
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)
Obtained plot has the same style, but is much simpler as there are only two levels of variable "Class": 'global' and 'local'获得的情节具有相同的风格,但更简单,因为变量“Class”只有两个级别:'global'和'local'
Question is : Is it any possibility to add legend within rgl or maybe some independent legend can be attached to the plot?问题是:是否有可能在 rgl 中添加图例,或者可以将一些独立的图例附加到情节中? Thanks in advance for help!
预先感谢您的帮助!
Answer is:答案是:
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)
The plot with labels according to classes:根据类别带有标签的图:
That's not a plot3d
image (unless perhaps you have another package loaded) , but looks like a scatter3d
rendering constructed with the scatter3d
function from the car
package by John Fox:这不是
plot3d
图像(除非您可能加载了另一个包),但看起来像是使用 John Fox 的car
包中的scatter3d
函数构建的scatter3d
渲染:
require(rgl)
require(car)
scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]],
point.col = as.numeric(as.factor(loadings[,4])), size = 10)
The scatter3d
function does depend on rgl functions but has quite a few customization options. scatter3d
函数确实依赖于 rgl 函数,但有很多自定义选项。 You offered no code to construct a "legend", so I played around with the example offered in rgl::text3d:您没有提供构建“图例”的代码,因此我使用了 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)
With the new data this is in response to the request for text and points:使用新数据,这是为了响应对文本和点的请求:
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)
I used the formula method to create groups, and 'removed' the group labels by turning them white because I could not figure out how to do this an easier way.我使用公式方法来创建组,并通过将它们变成白色来“删除”组标签,因为我不知道如何以更简单的方式做到这一点。 Then I created a legend using
legend3d
... which was very simple.然后我使用
legend3d
创建了一个图例......这非常简单。 Just make sure point and legend colours match.只要确保点和图例颜色匹配。 Maybe this is clunky, but it did what I needed it to do!
也许这很笨重,但它做了我需要它做的事情!
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.