14  3-Dimensional

I originally tried to use rgl to show a 3D face, but after I updated my Mac OS to Monterey, it no longer supports Open GL, so I’ve removed that part. Instead, I’ll make some 3-dimensional data from a multivariate normal distribution using faux, and 3D plot it using plotly.

Setup
library(plotly)
library(faux)
library(dplyr)

14.0.1 Simulate multivariate distribution

In faux, you can set the correlations using a matrix, or just the upper right triangle values as a vector. So c(.1, .2, .3) would mean that \(r_{xy} = .1\), \(r_{xz} = .2\), and \(r_{yz} = .3\).

Code
dat_ppp <- faux::rnorm_multi(
  r = c(.9, .9, .9),
  varnames = c("x", "y", "z")
) %>%
  mutate(cors = "+++")

dat_nnp <- faux::rnorm_multi(
  r = c(-.9, -.9, .9),
  varnames = c("x", "y", "z")
) %>%
  mutate(cors = "--+")

dat_pnn <- faux::rnorm_multi(
  r = c(.9, -.9, -.9),
  varnames = c("x", "y", "z")
) %>%
  mutate(cors = "+--")

dat <- bind_rows(dat_ppp, dat_nnp, dat_pnn)

14.0.2 Marker style

Next, set up the marker style.

Code
#set up the marker style
marker_style = list(
    line = list(
      color = "#444", 
      width = 1
    ), 
    opacity = 0.5,
    size = 3
  )

14.0.3 3D Plot

Finally, make the plot and add markers. These plots look cool, but I find them pretty hard for inference with data.

Code
# plot and add markers
plot_ly(data = dat,
        x = ~x, y = ~y, z = ~z, 
        color = ~cors,
        marker = marker_style) %>%
  add_markers()