如何在R中的3d图中绘制线条

时间:2017-04-20 19:50:11

标签: r 3d plotly scatter-plot scatter3d

我在R中制作了这两个三维图形,具有两个不同的功能:

x <- c(0,50,100,150,200,250,300,350,400,450)
y <- c(0,50,100,150,200,250,300,350,400,450) 
z <- c(1,2,1,1,2,1,2,1,2,1)

plot_ly(x=x,y=y,z=z)
scatterplot3d(x, y, z)

我想在这两个图中绘制,以下几行和它们之间的交叉区域:

在两个图表中绘制的线条

Lines plotted in both graphs

1 个答案:

答案 0 :(得分:1)

根据线条和情节的交集,这样的事情会做:

library(scatterplot3d)
library(plotly)

x <- c(0,50,100,150,200,250,300,350,400,450)
y <- c(0,50,100,150,200,250,300,350,400,450) 
z <- c(1,2,1,1,2,1,2,1,2,1)
data <- data.frame(x=x,y=y,z=z)
line <- data.frame(x = rep(200,5), y = seq(0,500,length.out = 5), z = rep(1,5))
line2 <- data.frame(x = rep(300,5), y = seq(0,500,length.out = 5), z = rep(1,5))
rect <- data.frame(expand.grid(x= c(200,300), y =c(200,300)),z = rep(1,4) )

plot_ly() %>% 
add_trace(data=data, x=x, y=y, z=z, type="scatter3d", mode="markers") %>% 
add_trace(line, x = line$x, y = line$y, z = line$z, type = 'scatter3d', mode = 'lines', line = list(color = 'rgb(1, 1, 1)', width = 1 , dash = 'dash', width = 4))  %>% 
add_trace(line2, x = line2$x, y = line2$y, z = line2$z, type = 'scatter3d', mode = 'lines',  line = list(color = 'rgb(1, 1, 1)', width = 1, dash = 'dash', width = 4))   %>% 
add_trace(line, x = line$y, y = line$x, z = line$z, type = 'scatter3d', mode = 'lines', line = list(color = 'rgb(1, 1, 1)', width = 1 , dash = 'dash', width = 4))  %>% 
add_trace(line2, x = line2$y, y = line2$x, z = line2$z, type = 'scatter3d', mode = 'lines',  line = list(color = 'rgb(1, 1, 1)', width = 1, dash = 'dash', width = 4))%>%  
add_trace(rect,  x=rect$x, y=rect$y, z=rect$z, type="mesh3d" ) 

情节结果:

enter image description here