Question

In using the function scatterplot3d() from the scatterplot3d package, grid=TRUE only plots the grid for XY-plane. A look at the function source code has only X and Y components. I want to draw the grid for YZ- and XZ- planes as well, as below in the picture:

Goal

I have searched the images for examples of R scatterplot3d images with these grids to no avail. I thought about s3d$plane3d but the planes that were generated look very messy.

I have not have experience editing codes or writing function in R. I thought I could add in arguments xy.grid, yz.grid, and xz.grid and then modify the original definition for grid, like this:

if (xy.grid) {
    i <- x.min:x.max
    segments(i, z.min, i + (yx.f * y.max), yz.f * y.max + 
                 z.min, col = col.grid, lty = lty.grid)
    i <- 0:y.max
    segments(x.min + (i * yx.f), i * yz.f + z.min, x.max + 
                 (i * yx.f), i * yz.f + z.min, col = col.grid, lty = lty.grid)
}


if (xz.grid) {
    i <- x.min:x.max
    segments(i, y.min, i + (zx.f * z.max), zy.f * z.max + 
                 y.min, col = col.grid, lty = lty.grid)
    i <- 0:z.max
    segments(x.min + (i * zx.f), i * zy.f + y.min, x.max + 
                 (i * zx.f), i * zy.f + y.min, col = col.grid, lty = lty.grid)
}
if (yz.grid) {
    i <- y.min:y.max
    segments(i, x.min, i + (yz.f * z.max), zx.f * z.max + 
                 x.min, col = col.grid, lty = lty.grid)
    i <- 0:z.max
    segments(y.min + (i * yz.f), i * zx.f + x.min, y.max + 
                 (i * yz.f), i * zx.f + x.min, col = col.grid, lty = lty.grid)
}

And I also try to add in this block of code in congruent fashion to x and z to define these y terms.

   y.range <- range(dat$y[is.finite(dat$y)], ylim)
    y.prty <- pretty(y.range, n = lab[1], min.n = max(1, min(0.5 * 
                                                                 lab[1], p.lab[1])))
    y.scal <- round(diff(y.prty[1:2]), digits = 12)
    dat$y <- dat$y/y.scal
    y.range <- range(y.prty)/y.scal
    y.max <- ceiling(y.range[2])
    y.min <- floor(y.range[1])
    if (!is.null(ylim)) {
        y.max <- max(y.max, ceiling(ylim[2]/y.scal))
        y.min <- min(y.min, floor(ylim[1]/y.scal))
    }
    y.range <- range(y.min, y.max)

However, when running this new code I encounter errors such as Error in segments(i, y.min, i + (zx.f * z.max), zy.f * z.max + y.min, : object 'zx.f' not found. I am not sure how and where this term is term is defined in the function code. I would appreciate some help in leading me to the right direction thanks!

I have also attached an original code for the scatterplot3d function here: scatterplot3d function source code

Was it helpful?

Solution

I create a new function, that add grid to xy and/or yz function. The argument grid now , can take a list like grid=c('xy','yz')

z <- seq(-10, 10, 0.01)
x <- cos(z)
y <- sin(z)
sactter.grid(x, y, z, highlight.3d=TRUE,
              col.axis="blue",
              grid=c('xy','xz','yz'),  ## add grid to all facets
          col.grid="lightblue")

enter image description here

You can find the source code of the new function, in this gist.

Here the code modified:

if ("xy" %in% grid || grid) {
        i <- x.min:x.max
        segments(i, z.min, i + (yx.f * y.max), yz.f * y.max + 
                            z.min, col = col.grid, lty = lty.grid)
        i <- 0:y.max
        segments(x.min + (i * yx.f), i * yz.f + z.min, x.max + 
                            (i * yx.f), i * yz.f + z.min, col = col.grid, lty = lty.grid)
    }
    if ("xz" %in% grid) {
        i <- x.min:x.max
        segments(i + (yx.f * y.max), yz.f * y.max + z.min, 
                         i + (yx.f * y.max), yz.f * y.max + z.max, 
                         col = col.grid, lty = lty.grid)
        temp <- yx.f * y.max
        temp1 <- yz.f * y.max
        i <- z.min:z.max
        segments(x.min + temp,temp1 + i, 
                         x.max + temp,temp1 + i , col = col.grid, lty = lty.grid)

    }

    if ("yz" %in% grid) {
        i <- 0:y.max
        segments(x.min + (i * yx.f), i * yz.f + z.min,  
                         x.min + (i * yx.f) ,i * yz.f + z.max,  
                         col = col.grid, lty = lty.grid)
        temp <- yx.f * y.max
        temp1 <- yz.f * y.max
        i <- z.min:z.max
        segments(x.min + temp,temp1 + i, 
                         x.min, i , col = col.grid, lty = lty.grid)



    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top