简体   繁体   English

R在3D散点图中的平面平坦

[英]R plotly flat surface in 3d scatterplot

I am trying to add a flat surface to this plot (Excuse the size, if I make it bigger the file is too large). 我正在尝试在此图上添加平坦的表面(请注意尺寸,如果我将其增大,则文件太大)。 The surface should be parallel to the ground with the third dimension equal zero. 该表面应平行于地面,且三维尺寸等于零。 It should go through the red points. 它应该通过红点。

Code to reproduce: 复制代码:

n.cases <- 240              
n.vars <- 4                
set.seed(26)               
eps <- rnorm(n.vars, 0, 1/4)
x <- matrix(rnorm(n.cases * (n.vars+2)), nrow=n.cases)
beta <- rbind(c(1,rep(0, n.vars)), c(0,rep(1, n.vars)), cbind(rep(0,n.vars), diag(eps)))
y <- x%*%beta             

data <- data.frame(y)
data.1.2 <- data[,1:2]
data.1.2$X3 <- integer(nrow(data.1.2))

data.1.2$group = 'First Iteration'

data.1.3 <- data[,1:3]
data.1.3$group = 'Second Iteration'

newdf <- rbind(data.1.2, data.1.3)

library(plotly)

plot_ly(x=newdf$X1, y=newdf$X2, z=newdf$X3, color = newdf$group, colors = c('#BF382A', '#0C4B8E')) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'First Dimension'),
                      yaxis = list(title = 'Second Dimension'),
                      zaxis = list(title = 'Third Dimension'),
                      bgcolor = "rgb(244, 244, 248)"))  

I tried to use add_surface , but without success so far. 我尝试使用add_surface ,但到目前为止没有成功。 I do not understand what kind of matrix the function needs. 我不了解该功能需要哪种矩阵。 I tried it with 我尝试过

matrix <- cbind(c(-5,-4,-3,-2,-1,0,1,2,3,4,5), c(-5,-4,-3,-2,-1,0,1,2,3,4,5), c(0,0,0,0,0,0,0,0,0,0,0))

and then adding the surface through 然后通过添加表面

%>%  add_surface(z=~matrix2)

but this does end up with 但这确实以

Error in traces[[i]][[obj]] : 
 attempt to select less than one element in get1index

I think I do not understand how the matrix has to look like yet. 我想我还不了解矩阵的外观。

Thank you! 谢谢!

I think you are looking for something like this: 我认为您正在寻找这样的东西:

matrix2 <- c(
  c(0,0,0,0),
  c(0,0,0,0),
  c(0,0,0,0),
  c(0,0,0,0)
)
dim(matrix2) <- c(4,4)

y <- c(-2,-1,0,1)
x <- c(-2,-1,0,1)
rownames(matrix2) <- x
colnames(matrix2) <- y


plot_ly() %>%
  add_markers(data = newdf, x=~X1, y=~X2, z=~X3, color = ~group, colors = c('#BF382A', '#0C4B8E')) %>%
  layout(scene = list(xaxis = list(title = 'First Dimension'),
                      yaxis = list(title = 'Second Dimension'),
                      zaxis = list(title = 'Third Dimension'),
                      bgcolor = "rgb(244, 244, 248)")) %>%  add_surface(z=~matrix2, x = ~x, y= ~y)

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

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