Add 3D abline to cloud plot in R's lattice package

ぐ巨炮叔叔 提交于 2019-12-12 14:25:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7699607/add-3d-abline-to-cloud-plot-in-rs-lattice-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!