3d 散点图中的 R plotly 平面
R plotly flat surface in 3d scatterplot
我正在尝试向 this plot 添加一个平面(请原谅尺寸,如果我把它变大则文件太大)。该表面应与地面平行,第三维等于零。它应该通过红点。
重现代码:
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)"))
我尝试使用 add_surface,但到目前为止没有成功。我不明白函数需要什么样的矩阵。我试过
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))
然后通过
添加曲面
%>% add_surface(z=~matrix2)
但这确实以
结束
Error in traces[[i]][[obj]] :
attempt to select less than one element in get1index
我想我还不明白矩阵的样子。
谢谢!
我想你正在寻找这样的东西:
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)
我正在尝试向 this plot 添加一个平面(请原谅尺寸,如果我把它变大则文件太大)。该表面应与地面平行,第三维等于零。它应该通过红点。
重现代码:
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)"))
我尝试使用 add_surface,但到目前为止没有成功。我不明白函数需要什么样的矩阵。我试过
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))
然后通过
添加曲面%>% add_surface(z=~matrix2)
但这确实以
结束Error in traces[[i]][[obj]] :
attempt to select less than one element in get1index
我想我还不明白矩阵的样子。
谢谢!
我想你正在寻找这样的东西:
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)