问题
x <-rnorm(500,50,2)
y <-rnorm(500,5,1)
z <-rnorm(500,6,1)
s3d <- scatterplot3d(x[z<6], y[z<6], z[z<6], zlim=range(z), color="darkgrey", col.axis="blue",col.grid="lightblue", main="scatterplot3d - 1", pch=20)
s3d$plane3d(6,0,0)
s3d$points3d(x[z>=6], y[z>=6], z[z>=6], pch=20)
The above code tells me how to add a plane 'z=6' to the 3d scatter plot.
The first question is: I'm wondering how to add a plane such as 'x=3' or 'y=2'.
R help file explains that
plane3d(Intercept, x.coef = NULL, y.coef = NULL, lty = "dashed", lty.box = NULL, ...). Instead of Intercept a vector containing 3 elements can be specified.
The second question is: I also wonder what the 'vector with 3 elements' instead of Intercept does and what is the role of x.coef and y.coef argument.
回答1:
Question 1: I would use a linear model to add planes, as I describe here and as the package authors use in the vignette:
plot3d <- scatterplot3d(x, y, z, ... )
model <- lm(y ~ x + z)
plot3d$plane3d(model)
You can specify the xyz intercepts manually, but I don't recommend it, as it produces somewhat odd behaviour. You can also construct complex mesh surfaces using the function intended for points, but as the authors state in the vignette:
Note that scatterplot3d is designed to generate scatter plots, not to draw surfaces, is not really user friendly for this purpose, for which we'd typically rather use R's persp function.
Question 2: The vector with three elements is a container for the xyz intercepts. You can specify them manually as you did above with s3d$plane3d(6,0,0)
. The x and y coefficients appear to be for mapping those two variables onto a plane.
To make specific planes manually, here is a suggestion from Uwe himself:
spd <- scatterplot3d(1:10, 1:10, 1:10)
# xy
spd$plane3d(0.3549896,0,0,lty="dotted")
# yz
x0 <- 5
xyz1 <- spd$xyz.convert(rep(x0, 6), rep(0, 6), seq(0, 10, by=2))
xyz2 <- spd$xyz.convert(rep(x0, 6), rep(10, 6), seq(0, 10, by=2))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
xyz1 <- spd$xyz.convert(rep(x0, 6), seq(0, 10, by=2), rep(0, 6))
xyz2 <- spd$xyz.convert(rep(x0, 6), seq(0, 10, by=2), rep(10, 6))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
# zx
y0 <- 6
xyz1 <- spd$xyz.convert(rep(0, 6), rep(y0, 6), seq(0, 10, by=2))
xyz2 <- spd$xyz.convert(rep(10, 6), rep(y0, 6), seq(0, 10, by=2))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
xyz1 <- spd$xyz.convert(seq(0, 10, by=2), rep(y0, 6), rep(0, 6))
xyz2 <- spd$xyz.convert(seq(0, 10, by=2), rep(y0, 6), rep(10, 6))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
The points are first sampled at a regular interval matching the grid in xyz space using their built-in xyz coordinate conversion function, before mapping the segments between them, producing grids:
来源:https://stackoverflow.com/questions/28633784/adding-a-plane-to-scatterplot3d