简体   繁体   English

Plot 在 R 中简化为 3D

[英]Plot simplices in 3D in R

I have a list of points and a list of simplices.我有一个点列表和一个单纯形列表。 I would like to plot the simplices in 3D given their vertices.我想 plot 3D 中的单纯形给定它们的顶点。 Essentially, I am looking for the equivalent of segment() in 3D.本质上,我正在寻找 3D 中的 segment() 等价物。

Example例子

Pts<-matrix(c(0,0,0,1,0,0,0,1,0,0,0,1),ncol =3,byrow=TRUE)
Simplex<-c(1,2,3,4)

So, I am looking for a way to input Pts and Simplex and getting a plot of the tetrahedron.因此,我正在寻找一种输入 Pts 和 Simplex 并获得四面体的 plot 的方法。

I've tried searching but so far the only possibility seems to write out the functions for the linear spaces and plot those.我试过搜索,但到目前为止,唯一的可能性似乎是写出线性空间和 plot 的函数。 Any tips will be highly appreciated.任何提示将不胜感激。

With the 'rgl' package:使用“rgl” package:

library(rgl)

vertices <- rbind(
  c(0, 0, 0),
  c(1, 0, 0),
  c(0, 1, 0),
  c(0, 0, 1)
)

faces <- combn(4,3)
for(f in 1:4){
  triangles3d(rbind(
    vertices[faces[1,f],],
    vertices[faces[2,f],],
    vertices[faces[3,f],]
  ), color="red", alpha=0.4)
}

在此处输入图像描述

You can add the edges and the vertices:您可以添加边和顶点:

# add edges as thin cylinders
edges <- combn(4, 2)
for(e in 1:6){
  shade3d(cylinder3d(rbind(vertices[edges[1,e],],vertices[edges[2,e],]), 
                     radius = 0.02, sides = 30), col="yellow")
}
# add vertices as small spheres
spheres3d(vertices, radius= 0.03, color = "yellow")

在此处输入图像描述

Not as pretty or flexible, but here's a base R version using persp and segments for fun:不那么漂亮或灵活,但这是一个基本的 R 版本,使用perspsegments来娱乐:

## empty perspective plot
tm <- persp(matrix(rep(0,4), nrow=2),
            xlim=c(-1,1), ylim=c(-1,1), zlim=c(-1,1),
            col="#00000000", border=NA, theta=30, phi=50, xlab="x")

## project points into 3d space
tpts <- data.frame(trans3d(pmat=tm, x=Pts[,1], y=Pts[,2], z=Pts[,3]))

## draw each segment
sgs <- combn(seq_len(nrow(tpts)), 2,
             FUN=function(r) unlist(tpts[r,]), simplify=FALSE)
lapply(sgs, function(x) segments(x[1], x[3], x[2], x[4], col="red"))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM