Ryland Grace
Ryland Grace

Reputation: 19

How to add spheres to my points in a 3d scatterplot in r

I'm looking to add spheres to the ends of my points in a 3d scatterplot in r, but cannot figure out how to do it. I'm using the default scatter3d function, and the image I get is attached. As stated previously, I would like to add spheres to the end of the blue and purple lines on the plane.

The code I used was:

library(car)
library(rgl)
scatter3d(dataset2$x1~dataset2$x2+dataset2$x3)

I was expecting an output something like the other image attached, with spheres at the end of the lines.
Output I am receiving:
Output I am receiving
Output I am expecting to get:
Output I am expecting to get

Edit: In a clean session, using the code provided by Ben:

library(car)
scatter3d(mpg ~ disp + hp, data = mtcars); mm <- transform(mtcars, mpg = mpg*1000, disp = disp*1000, hp = hp*1000); scatter3d(mpg ~ disp + hp, data = mm)

I get the same issue, the sessionInfo() result is as follows:

R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

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

other attached packages:
[1] car_3.1-3     carData_3.0-5

loaded via a namespace (and not attached):
 [1] rgl_1.3.17        digest_0.6.37     Formula_1.2-5     R6_2.5.1         
 [5] base64enc_0.1-3   fastmap_1.2.0     Matrix_1.7-1      mgcv_1.9-1       
 [9] xfun_0.50         lattice_0.22-6    magrittr_2.0.3    splines_4.4.2    
[13] abind_1.4-8       knitr_1.49        htmltools_0.5.8.1 cli_3.6.3        
[17] grid_4.4.2        compiler_4.4.2    tools_4.4.2       nlme_3.1-166     
[21] evaluate_1.0.3    rlang_1.1.5       jsonlite_1.8.9    htmlwidgets_1.6.4
[25] MASS_7.3-61 

I should also note, I don't think it is a problem of the parameters, as others using the same data (who had already installed rgl and car, whereas I had only just installed these libraries), had the spheres running the exact same line of code.

Perhaps the reason for this issue, the spheres3d function from the rgl library is not working, and only returns a blank image when the example("spheres3d") is ran. It could be helpful to get this working.

Upvotes: 1

Views: 53

Answers (0)

Related Questions