2017-04-20 148 views
1

我中的R由这两个3D图形,用两个不同的功能:如何绘制在三维图中的线中的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

+0

可以使用具有type = “L” lines3d()或points3d()从包rgl –

+0

我不清楚你在问什么,但是,plot_ly和scatterplot3d只是通过不同的方式做同样的事情。如果你想添加一个三维线的情节使用:data < - data.frame(x = x,y = y,z = z); (数据,x =〜x,y =〜y,z =〜z,type ='scatter3d',mode ='lines') –

回答

1

按照该线和交叉口plotly,这样的事情就可以了:

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") 

结果与plotly:

enter image description here

+0

非常感谢!这工作得很好,这正是我需要的!有没有一种方法可以绘制这个突出显示区域的整个3d列? – lalatei

+0

你是指在z轴上绘制一个包含1到2阴影体积的棱镜? –

+0

是的,那正是我需要的! – lalatei