I have a series of points that exist in 3 dimensional space (x, y, and z) and an adjacency matrix that determines connections between those points (see example below). How would I plot that? Thanks!
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)
There may be a more elegant way to handle the adjacency matrix, but as far as I can tell rgl::segments3d()
turns sequential points into segments, so you need to repeat points for each connection. The below approach is slightly redundant; set the upper or lower triangle to 0 if you like, but you won't be able to see the difference anyway since the segments will overplot.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.