I want to add a 3D abline to a cloud scatterplot in R's lattice package. Here's a subset of my data (3 variables all between 0,1):

dat <- structure(c(0.413, 0.879, 0.016, 0.631, 0.669, 0.048, 1, 0.004, 0.523, 0.001, 
0.271, 0.306, 0.014, 0.008, 0.001, 0.023, 0.670, 0.027, 0.291, 0.709, 
0.002, 0.003, 0.611, 0.024, 0.580, 0.755, 1, 0.003, 0.038, 0.143, 0.214, 
0.161, 0.008, 0.027, 0.109, 0.026, 0.229, 0.006, 0.377, 0.191, 0.724, 
0.119, 0.203, 0.002, 0.309, 0.011, 0.141, 0.009, 0.340, 0.152, 0.545, 
0.001, 0.217, 0.132, 0.839, 0.052, 0.745, 0.001, 1, 0.273), .Dim = c(20L, 3L))

Here's the cloud plot:

# cloud plot
trellis.par.set("axis.line", list(col="transparent")) 
cloud(dat[, 1] ~ dat[, 2] + dat[, 3], pch=16, col="darkorange", groups=NULL, cex=0.8, 
    screen=list(z = 30, x = -70, y = 0),
    scales=list(arrows=FALSE, cex=0.6, col="black", font=3, tck=0.6, distance=1) ) 

I want to add a dashed grey line between 0,0,0 and 1,1,1 (i.e., diagonally through the plot). I know I can change the points to lines using "type="l", panel.3d.cloud=panel.3dscatter", but I can't see a way to add extra points/lines to the plot using this.

Here's an example of what I want to achieve using scatterplot3d:

# scatterplot3d
s3d <- scatterplot3d(dat, type="p", color="darkorange", angle=55, scale.y=0.7,
    pch=16, col.axis="blue", col.grid="lightblue") 

# add line 
s3d$points3d(c(0,1), c(0,1), c(0,1), col="grey", type="l", lty=2)

I want to do this with a cloud plot to control the angle at which I view the plot (scatterplot3d doesn't allow me to have the 0,0,0 corner of the plot facing). Thanks for any suggestions.

有帮助吗?

解决方案

Inelegant and probably fragile, but this seems to work ...

cloud(dat[, 1] ~ dat[, 2] + dat[, 3], pch=16, col="darkorange", 
        groups=NULL, cex=0.8, 
    screen=list(z = 30, x = -70, y = 0),
    scales=list(arrows=FALSE, cex=0.6, col="black", font=3, 
                tck=0.6, distance=1) ,
      panel=function(...) {
        L <- list(...)
        L$x <- L$y <- L$z <- c(0,1)
        L$type <- "l"
        L$col <- "gray"
        L$lty <- 2
        do.call(panel.cloud,L)
        p <- panel.cloud(...)
      })

One thing to keep in mind is that this will not do hidden point/line removal, so the line will be either in front of all of the points or behind them all; in this (edited) version, do.call(panel.cloud,L) is first so the points will obscure the line rather than vice versa. If you want hidden line removal then I believe rgl is your only option ... very powerful but not as pretty and with a much more primitive interface.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top