Plot 3D figures for list data

Asked

Viewed 235 times

7

I have a list a with three matrices and one vector v with any three frequencies (positive reals), these matrices form triangles through a function I created pplot. I wish to add vector information v to build prisms, that is, to go from 2D to 3D. Someone has some idea or tip of how to do?

Below follows the function pplot and a Toy problem:

library(ggplot2)
pplot <- function(polygon){
  polygon <- lapply(polygon, function(x) {colnames(x) <- NULL; x})
  vertex_number = nrow(polygon[[1]])
  g = ggplot2::ggplot()
  names(polygon) = 1:length(polygon)
  k <- plyr::ldply(polygon, function(x) data.frame(x))
  g <- ggplot2::ggplot(k, ggplot2::aes(x = X1, y = X2, group = .id)) + ggplot2::geom_polygon(colour = "black", fill = NA)
  return(g)
}

a <- list()
b1 <- matrix(rnorm(6), ncol = 2)
b2 <- matrix(rnorm(6), ncol = 2)
b3 <- matrix(rnorm(6), ncol = 2)

a[[1]] <- b1
a[[2]] <- b2
a[[3]] <- b3

v <- c(.3, .5, .1)
#Para exemplificar a funcao que eh 2D
pplot(a) 

Note: Mandatory data are lists.

Desired answer

For example, forming a prism through b1 and v[1], then the basis of the prism is b1 and height (h) is v[1]. The same reasoning goes to b2 with v[2] and b3 with v[3]. inserir a descrição da imagem aqui

  • As soon as I have more time I will try. I think a good way out is to use plotly.

  • Thanks @Danielfalbel, I’ll try here if I get a good answer I put here. Abs.

  • I couldn’t visualize the desired response. The elements a[[1]], a[[2]] and a[[3]] of the list a are the coordinates of the prism faces? This prism is necessarily a tetrahedron? What is the vector for v, since it seems to make no difference at all? What does it mean that this vector v will make "go from 2D to 3D"?

  • a[[1]] is the basis of a prism and v[1] is the time, a[[2]] is the basis and v[2] is the height of another prism. I would like these three prisms to be plotted on the same graph (superimposed).

1 answer

2


Dear, based on the answer I got in: https://stackoverflow.com/questions/40614888/plot-3d-prisms-using-ggplot2-and-plotly/41433697#41433697 I adapted the method to plot a polygonal base prism with any number of sides.

library(rgl)
a <- list()
b1 <- matrix(rnorm(6), ncol = 2)
b2 <- matrix(rnorm(6), ncol = 2)
b3 <- matrix(rnorm(6), ncol = 2)

a[[1]] <- b1
a[[2]] <- b2
a[[3]] <- b3

v <- c(.3, .5, .1) #Altura

pprism <- function(a, h){
  # general loop to plot every prism
  for(i in 1:length(h)){
    # transform matrizes to data.frames and add height column 
    # -> separation of top and bottom triangle
    sides <- nrow(a[[1]]) - 1 
    top <- data.frame(a[[i]], h[i]) 
    bottom <- data.frame(a[[i]], 0) 
    # adjust colnames to axis names
    colnames(top) <- c("x", "y", "z") 
    colnames(bottom) <- c("x", "y", "z") 
    # plot triangles (as wireframes)
    lines3d(bottom, front = "line", back = "line")
    lines3d(top, front = "line", back = "line")
    # plot vertical lines to connect the triangles
    for(i in 0:sides){
      segments3d(
        x = c(bottom$x[1+i], top$x[1+i]),
        y = c(bottom$y[1+i], top$y[1+i]),
        z = c(bottom$z[1+i], top$z[1+i])
      )
    }
  }
  #### add coordinate system ####
  axes3d()
}
pprism(a,v)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.