简体   繁体   English

Scatter3D 添加多个曲面

[英]Scatter3D add multiple surfaces

I am trying to add multiple surfaces to one scatter3d plot in R. Here is an example from scatter3d documentation:我正在尝试将多个曲面添加到 R 中的一个scatter3d plot。这是scatter3d文档中的一个示例:

library(plot3D)

with (mtcars, {
  
  # linear regression
  fit <- lm(mpg ~ wt + disp)
  
  # predict values on regular xy grid
  wt.pred <- seq(1.5, 5.5, length.out = 30)
  disp.pred <- seq(71, 472, length.out = 30)
  xy <- expand.grid(wt = wt.pred, 
                    disp = disp.pred)
  
  mpg.pred <- matrix (nrow = 30, ncol = 30, 
                      data = predict(fit, newdata = data.frame(xy), 
                                     interval = "prediction")[,1])
  
  # fitted points for droplines to surface
  fitpoints <- predict(fit) 
  
  scatter3D(z = mpg, x = wt, y = disp, pch = 18, cex = 2, 
            theta = 20, phi = 20, ticktype = "detailed",
            xlab = "wt", ylab = "disp", zlab = "mpg",  
            surf = list(x = wt.pred, y = disp.pred, z = mpg.pred,  
                        facets = NA, fit = fitpoints),
            main = "mtcars")
  
})

which produces the following plot:产生以下 plot:

在此处输入图像描述

Let's say I wanted to add two more surfaces that are:假设我想再添加两个表面:

surf1<-mpg.pred+5
surf2<-mpg.pred-5

How would I go about doing that?我将如何 go 这样做?

You can use surf3D with add = TRUE to add the extra surfaces:您可以使用带有add = TRUEsurf3D添加额外的曲面:

library(plot3D)

with (mtcars, {
  
  # linear regression
  fit <- lm(mpg ~ wt + disp)
  
  # predict values on regular xy grid
  wt.pred <- seq(1.5, 5.5, length.out = 30)
  disp.pred <- seq(71, 472, length.out = 30)
  xy <- expand.grid(wt = wt.pred, 
                    disp = disp.pred)
  
  mpg.pred <- matrix (nrow = 30, ncol = 30, 
                      data = predict(fit, newdata = data.frame(xy), 
                                     interval = "prediction")[,1])
  
  # fitted points for droplines to surface
  fitpoints <- predict(fit) 
  
  p1 <- scatter3D(z = mpg, x = wt, y = disp, pch = 18, cex = 2,
               theta = 20, phi = 20, ticktype = "detailed",
               xlab = "wt", ylab = "disp", zlab = "mpg",
               surf = list(x = wt.pred, y = disp.pred, z = mpg.pred,
                           facets = NA, fit = fitpoints),
               main = "mtcars")
  p2 <- surf3D(x = matrix(wt.pred, 30,30),
            y = matrix(disp.pred, 30,30,byrow=TRUE), z = mpg.pred + 5, facets = NA, add = TRUE)
  p3 <- surf3D(x = matrix(wt.pred, 30,30),
            y = matrix(disp.pred, 30,30,byrow=TRUE), z = mpg.pred - 5, facets = NA, add = TRUE)
})

Created on 2022-04-27 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-04-27

The legend and color schemes are a little bit wrong;图例和配色方案有点不对; you could perhaps fix them, but I don't know how.你也许可以修复它们,但我不知道如何。 Using colkey=list(plot=FALSE) in the two surf3D calls helps a bit, but the color scheme is still wrong for the extra planes.在两个surf3D调用中使用colkey=list(plot=FALSE)会有所帮助,但是额外平面的配色方案仍然是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM