绘制 3D 网络
Plotting 3D Network
我有一系列存在于 3 维 space(x、y 和 z)中的点和一个确定这些点之间连接的邻接矩阵(请参见下面的示例)。我将如何绘制它?谢谢!
points = matrix(c(2,3,2, 5,4,9, 4,1,8), byrow = TRUE, ncol = 3) #each row is a point and the colums are x, y, and z respectively
adj_mat = matrix(c(0,1,0, 1,0,1, 0,1,0), byrow = TRUE, ncol = 3)
可能有更优雅的方法来处理邻接矩阵,但据我所知,rgl::segments3d()
将连续的点变成段,因此您需要为每个连接重复点。下面的方法有点多余;如果您愿意,可以将上三角或下三角设置为 0,但无论如何您都看不到差异,因为这些线段会重叠。
points = matrix(
c(2,3,2, 5,4,9, 4,1,8),
byrow = TRUE, ncol = 3,
dimnames = list(NULL, c('x', 'y', 'z'))
)
adj_mat = matrix(c(0,1,0, 1,0,1, 0,1,0), byrow = TRUE, ncol = 3)
segments <- points[as.vector(matrix(
c(row(points)[as.logical(adj_mat)],
col(points)[as.logical(adj_mat)]),
byrow = TRUE, nrow = 2
)), ]
library(rgl)
plot3d(points)
segments3d(segments)
我有一系列存在于 3 维 space(x、y 和 z)中的点和一个确定这些点之间连接的邻接矩阵(请参见下面的示例)。我将如何绘制它?谢谢!
points = matrix(c(2,3,2, 5,4,9, 4,1,8), byrow = TRUE, ncol = 3) #each row is a point and the colums are x, y, and z respectively
adj_mat = matrix(c(0,1,0, 1,0,1, 0,1,0), byrow = TRUE, ncol = 3)
可能有更优雅的方法来处理邻接矩阵,但据我所知,rgl::segments3d()
将连续的点变成段,因此您需要为每个连接重复点。下面的方法有点多余;如果您愿意,可以将上三角或下三角设置为 0,但无论如何您都看不到差异,因为这些线段会重叠。
points = matrix(
c(2,3,2, 5,4,9, 4,1,8),
byrow = TRUE, ncol = 3,
dimnames = list(NULL, c('x', 'y', 'z'))
)
adj_mat = matrix(c(0,1,0, 1,0,1, 0,1,0), byrow = TRUE, ncol = 3)
segments <- points[as.vector(matrix(
c(row(points)[as.logical(adj_mat)],
col(points)[as.logical(adj_mat)]),
byrow = TRUE, nrow = 2
)), ]
library(rgl)
plot3d(points)
segments3d(segments)