0

I'm using the plot3d function in the library rgl. Suppose my data looks something like this.

    x <- c(1,2,3,4,5)
    y <- c(4,2,1,4,2)
    z <- c(2,2,4,5,1)

    x2 <- c(1,5,2,3,4)
    y2 <- c(2,3,4,1,2)
    z2 <- c(3,4,2,3,1)

    plot3d(x, y, z)
    plot3d(x2, y2, z2)

Using the commands above would give me 2 separate plots. How can I plot both datasets on the same graph? Also I would like to use different symbols for the points in the two different data sets.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Adrian
  • 9,229
  • 24
  • 74
  • 132
  • I don't think there is a way to get two spinnable plots in the same window. Assuming you want to show just a 2d version with fixed-orientation then use rgl.snapshot() and assemble. If you want two windows that you can switch back and forth with then http://stackoverflow.com/questions/14712497/plot3d-having-two-plots-at-once and perhaps embedding in a knitr doc: http://stackoverflow.com/questions/23021317/rottation-of-r-3d-plots-not-working-properly-with-knitr-and-webgl – IRTFM Sep 10 '14 at 20:00
  • Stack points in a matrix and use `col = rep(c("red", "black", each = 3))`? – Roman Luštrik Sep 10 '14 at 20:34

1 Answers1

0

I just wrote a function cube and an accompanying one that might be sufficient for your needs:

require(rgl); library('magrittr')
cube <- function(x=0,y=0,z=0, bordered=TRUE, 
                 filled = TRUE, lwd=2, scale=1,
                 fillcol = gray(.95),
                 bordercol ='black', ...) {

  mytetra <- cube3d()

  # Reduce size to unit
  mytetra$vb[4,] <- mytetra$vb[4,]/scale*2

  for (i in 1:length(x)) {
    # Add cube border
    if (bordered) {
      btetra <- mytetra
      btetra$material$lwd <- lwd
      btetra$material$front <- 'line'
      btetra$material$back <- 'line'
      btetra %>% translate3d(x[i], y[i], z[i]) %>% shade3d
    }
    # Add cube fill
    if (filled) {
      ftetra <- mytetra
      ftetra$vb[4,] <- ftetra$vb[4,]*1.01
      ftetra$material$col <- fillcol
      ftetra %>% translate3d(x[i], y[i], z[i]) %>% shade3d
    }
  }
}

tetra <- function(x=0,y=0,z=0, bordered=TRUE, 
                             filled = TRUE, lwd=2, scale=1,
                             fillcol = gray(.95),
                             bordercol ='black', ...) {

  mytetra <- tetrahedron3d()

  # Reduce size to unit
  mytetra$vb[4,] <- mytetra$vb[4,]/scale*2

  for (i in 1:length(x)) {
    # Add cube border
    if (bordered) {
      btetra <- mytetra
      btetra$material$lwd <- lwd
      btetra$material$front <- 'line'
      btetra$material$back <- 'line'
      btetra %>% translate3d(x[i], y[i], z[i]) %>% shade3d
    }
    # Add cube fill
    if (filled) {
      ftetra <- mytetra
      ftetra$vb[4,] <- ftetra$vb[4,]*1.01
      ftetra$material$col <- fillcol
      ftetra %>% translate3d(x[i], y[i], z[i]) %>% shade3d
    }
  }
}

plot3d(x,y,z)
tetra(x,y,z, scale=1/2)
cube(x2,y2,z2, scale=1/2)

Two overlapping plotted shape sets.

Francis Smart
  • 3,875
  • 6
  • 32
  • 58