文章目录
- graphics::persp
- lattice::wireframe
- rgl::plot3d
- rgl::surface3d
- scatterplot3d::scatterplot3d
graphics::persp
x <- seq(-10, 10, length.out = 30)
y <- x
f <- function(x, y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
z <- outer(x, y, f)
z[is.na(z)] <- 1
op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
ltheta = 120, shade = 0.75, ticktype = "detailed",
xlab = "X", ylab = "Y", zlab = "Sinc( r )"
) -> res
round(res, 3)


lattice::wireframe
library(lattice)
d <- expand.grid(x =10:60, y = 5:40,g=2:3)
d$z <-log((d$x^d$g+d$y^2)*d$g)
wireframe(z~x*y,
data = d,
shade=T,
groups=g,
drape=T,
colorkey=T,
scales=list(arrows=T),
screen=list(z=45,x=-60)
)

rgl::plot3d
library(rgl)
d <- expand.grid(x =10:60, y = 5:40,g=2:3)
d$z <-log((d$x^d$g+d$y^2)*d$g)
plot3d(d$x,d$y,d$z,
type="s",
size = 0.5,
col=terrain.colors(length(d$z)))

plot3d()所绘制的图形是可以用鼠标调整视角的。
plot3d(lm(mpg ~ wt + I(wt^2) + qsec,
data = mtcars),
plane.col =topo.colors(1))

rgl::surface3d
z <- 4 * volcano
x <- 8 * (1:nrow(z))
y <- 6 * (1:ncol(z))
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- topo.colors(zlen)
col <- colorlut[ z - zlim[1] + 1 ]
open3d()
surface3d(x, y, z, color = col, back = "lines")

scatterplot3d::scatterplot3d

data(trees)
s3d <- scatterplot3d(trees,
type="p",
highlight.3d=TRUE,
angle=60,
pch=16,
zlim = c(0,80),
col.grid="lightblue",
main="scatterplot3d")
tree_lm <- lm(Volume ~ Girth + Height,data = trees)
s3d$plane3d(tree_lm, lty.box = "solid")
