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

查看:26
本文介绍了在 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天全站免登陆