I have the following code in R which draws a 3D plot
library(mgcViz)
library(rgl)
n <- 500
x <- rnorm(n); y <- rnorm(n); z <- rnorm(n)
ob <- (x-z)^2 (y-z)^2 rnorm(n)
b <- gam(ob ~ s(x, y, z))
b <- getViz(b)
plotRGL(sm(b, 1), fix = c("z" = 0), residuals = TRUE)
The here is the graph that I create:
How can I add a simple horizontal plane on z=5?
CodePudding user response:
You can use the planes3d()
function from rgl
to do that. It uses the plane equation $a x b y c z d = 0$, so z=5
would have a=0, b=0, c=1, d=-5
. For example,
library(mgcViz)
#> Loading required package: mgcv
#> Loading required package: nlme
#> This is mgcv 1.8-40. For overview type 'help("mgcv-package")'.
#> Loading required package: qgam
#> Loading required package: ggplot2
#> Registered S3 method overwritten by 'GGally':
#> method from
#> .gg ggplot2
#> Registered S3 method overwritten by 'mgcViz':
#> method from
#> .gg GGally
#>
#> Attaching package: 'mgcViz'
#> The following objects are masked from 'package:stats':
#>
#> qqline, qqnorm, qqplot
library(rgl)
n <- 500
x <- rnorm(n); y <- rnorm(n); z <- rnorm(n)
ob <- (x-z)^2 (y-z)^2 rnorm(n)
b <- gam(ob ~ s(x, y, z))
b <- getViz(b)
plotRGL(sm(b, 1), fix = c("z" = 0), residuals = TRUE)
planes3d(0,0,1,-5, col = "gray")
Created on 2022-06-24 by the reprex package (v2.0.1)
Note that you can set the plane to be semi-transparent by setting an alpha value between 0 and 1, but rgl
doesn't handle intersecting transparent objects very well, so it's unlikely to give nice results.
It also appears that rgl
isn't working so well with reprex
. You shouldn't see the doubled axis labels. This is fixed in the devel version available on Github.