在RGL中将立方体绘制到3D散点图中 [英] draw cube into 3D scatterplot in RGL

查看:166
本文介绍了在RGL中将立方体绘制到3D散点图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向3D散点图中添加较小的立方体/网格(具有指定的边长)。我希望将多维数据集放置在原点。我将如何去做?我玩过cube3d(),但是我似乎无法将多维数据集正确放置或使其成为网格(这样我就可以看到它包含的数据点。这就是我所拥有的:

I'm trying to add a smaller cube/mesh (with specified side length) to a 3D scatterplot. I'd like for the cube to be positioned at the origin. How would I go about doing that? I've played around with cube3d() but I can't seem to position the cube right nor make it a mesh (so I can see the data points it contains. Here's what I have:

library(rgl)
x <- runif(100)
y <- runif(100)
z <- runif(100)
plot3d(x,y,z, type="p", col="red", xlab="x", ylab="y", zlab="z", site=5, lwd=15)


推荐答案

有一个 cube3d 函数,默认情况下会返回一个列表对象(但不绘制它),该对象表示跨越x:[-1,1]; y:[-1,1]; z:[- 1,1]。如果将颜色应用到侧面,则默认情况下将是纯色。您需要使用'alpha'使侧面透明(请参见?rgl.materials )。因此,如果我们从您使用的情节开始:

There is a cube3d function that by default returns a list object (but does not plot it) that represents a cube spanning x:[-1,1]; y:[-1,1]; z:[-1,1]. If you apply a color to the sides, it will be solid by default. You need to make the sides transparent with 'alpha" (see ?rgl.materials). So if we start out with the plot you used:

library(rgl)
x <- runif(100)
y <- runif(100)
z <- runif(100)
plot3d(x,y,z, type="p", col="red", xlab="x", ylab="y", zlab="z", site=5, lwd=15)
c3d <- cube3d(color="red", alpha=0.1)  # nothing happens yet
c3d   # Look at structure
shade3d(c3d)   # render the object

这会将图扩展为上述透明红色立方体的默认尺寸。顶点位于$ vb元素的前三行中的xyz位置:

This expands the plot to the default dimensions of the transparent red cube mentioned above. The vertices are at the xyz location in the first three rows of the $vb element:

c3b$vb
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]   -1    1   -1    1   -1    1   -1    1
[2,]   -1   -1    1    1   -1   -1    1    1
[3,]   -1   -1   -1   -1    1    1    1    1
[4,]    1    1    1    1    1    1    1    1

现在制作另一个在原点具有一个顶点的立方体,我能想到的最快方法就是将所有-1都设置为0:

Now to make another cube that has one of its vertices at the origin, the quickest way I could think of was to set all the -1's to 0:

 c3d.origin <- cube3d(color="red", alpha=0.1)
 c3d.origin$vb [c3d.origin$vb == -1] <- 0
 shade3d(c3d.origin)
 rgl.snapshot("cubes3d.png")

这篇关于在RGL中将立方体绘制到3D散点图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆