添加一个传奇scatter3d剧情 [英] Adding a legend to scatter3d plot
问题描述
其中一个可能的包交互式3D绘图是RGL。我想这样做是有颜色编码根据一些因素变量构建3D散点图。三维立体散点图是导致从偏最小二乘回归分析的载荷。
One of the possible packages for interactive 3D plots is rgl. What I would like to do is to build 3D scatterplot with color coding according to some factor variable. The 3D dimensional scatterplot is for resulting loadings from plsr analysis.
由此产生的剧情看起来像
The resulting plot looks like
在示例数据是在一个表:
> 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'
的问题是: 这是任何可能性在增加RGL传说或者一些独立的传说可以连接到的情节? 预先感谢帮助!
Question is: Is it any possibility to add legend within rgl or maybe some independent legend can be attached to the plot? Thanks in advance for help!
答案是:
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:
推荐答案
这不是一个 PLOT3D
图像(除非你也许已经加载另包),但看起来像一个 scatter3d
渲染从车
包由 scatter3d
函数构造约翰·福克斯:
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:
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功能,但有相当多的自定义选项。你没有提供code构建一个传奇,所以我在所提供的示例中发挥各地RGL :: text3d:
The scatter3d
function does depend on rgl functions but has quite a few customization options. You offered no code to construct a "legend", so I played around with the example offered in 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)
这篇关于添加一个传奇scatter3d剧情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!