0

I'm trying to add line segments to a cloud plot with the package lattice.

I've read the lattice documentation and I've found the functions lsegments() and panel.segments() but didn't manage to find a solution. Actually, I'm not sure whether these functions can be used in a 3D plot made with the cloud() function. I have x0, y0, x1, and y1 coordinate parameters to set. But how do I set my z coordinate for the line segments?

My data:

d <- structure(list(V1 = c(-0.231868485424126, 0.260991275378284, 
-0.973129576550547, 0.150224354474776, -0.743673301914161, 
0.21938366130134, 
0.899845151352479, 0.0808627438470515, 0.317569956044341, 
-0.481908931028419
), V2 = c(-0.964917334664869, 0.629592806926283, 0.155210445495851, 
-0.808297399258872, 0.38884655631618, -0.960935067863847, 
0.18526488864338, 
-0.701769619946431, -0.114753054406819, -0.737619553095911), 
V3 = c(0.123172816523645, -0.731776230580839, 0.170083934724984, 
0.569287236528258, -0.543827523817191, -0.168744198429227, 
0.394912173303622, 0.707799842595184, 0.941265668938531, 
0.472949444534652)), class = "data.frame", row.names = c(NA, 
-10L))

My vectors for the segments:

from <- c(4, 1, 1, 1, 8, 3, 7, 2, 3)
to <- c(8, 10, 6, 4, 9, 5, 9, 5, 10)

How my goal looks like in 2D:

plot(d[,1:2])
segments(d[from,1], d[from,2], d[to,1], d[to,2],col="black", lty=3 )

enter image description here

What I manage to do so far with lattice:

lattice::cloud(V3~V1+V2, data = d, pch= 19,
       screen=list(z = 30, x = -70, y = 0),
       scales=list(arrows=FALSE, cex=0.6, col="black", font=3,
                   tck=0.6, distance=1) ,
       drop.unused.levels = FALSE,

       panel = function(...){
         lattice::panel.cloud(...)
         lattice::panel.segments(x0 = d[from,1], y0 = d[from,2], x1 = 
d[to,1], y1 = d[to,2], z0=d[from,3], z1=d[to,3],  lwd=2, col="blue" )
       })

I know that there is no parameters z0 or z1. I put them only as a illustration of what I intend to do. Even though, this code produces the following graphic:

enter image description here

There is a weird blue line in the upright.

Other information:

I've also read the following SO questions, but I couldn't find a solution for my problem:

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

How to plot segments or arrows with Lattice in R? (from a Wide format dataset)

Plotting a wireframe AND a cloud with lattice in R

Sesion Info:

R version 3.5.1 (2018-07-02)
Platform: x86_64-suse-linux-gnu (64-bit)
Running under: openSUSE Leap 15.0

Matrix products: default
BLAS: /usr/lib64/R/lib/libRblas.so
LAPACK: /usr/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               
LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     
LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  
LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.20-35     MultiRNG_1.1        KODAMA_1.4          
bindrcpp_0.2.2      prodlim_2018.04.18  dplyr_0.7.6         
RcppMLPACK_1.0.10-6
[8] Rcpp_0.12.18       

loaded via a namespace (and not attached):
 [1] rstudioapi_0.7   bindr_0.1.1      magrittr_1.5     splines_3.5.1    
tidyselect_0.2.4 R6_2.2.2         rlang_0.2.1      fansi_0.2.3     
 [9] tools_3.5.1      grid_3.5.1       checkmate_1.8.5  utf8_1.1.4       
cli_1.0.0        yaml_2.2.0       survival_2.41-3  assertthat_0.2.0
[17] tibble_1.4.2     crayon_1.3.4     Matrix_1.2-14    lava_1.6.3       
purrr_0.2.5      glue_1.3.0       compiler_3.5.1   pillar_1.3.0    
[25] backports_1.1.2  BBmisc_1.11      pkgconfig_2.0.1
allanvc
  • 1,096
  • 10
  • 23
  • 1
    It's not lattice but rather base graphics however and it is then possible to add segments to scatterplot3d graphs: https://stackoverflow.com/questions/13495502/plotting-lines-between-two-points-in-3d – IRTFM Sep 01 '18 at 22:11
  • @42- Thank you for your suggestion. I will try that alternative. – allanvc Sep 01 '18 at 22:54

0 Answers0