简体   繁体   English

在 R 中生成 3D 曲面图

[英]Generate 3D surface plot in R

I have a data frame which consists of 4 variables A , B , C , D , I need to plot 3D surface plot for x = A , y = B , z = C .我有一个由 4 个变量ABCD组成的数据框,我需要为x = Ay = Bz = C绘制 3D 曲面图。 My problem is the plot should contain 3 surfaces which is with respect to the values in D ie D has values of 0 , 1 and -1 I need to have 3 surfaces for 3 different values of D .我的问题是小区应含有3 surfaces ,其相对于中的值DD具有值01-1我需要有3个不同的值3面D

I tried by sub setting the data frame into 3 different dataframes with respect to the values of D and adding surface to plot_ly function but it doesnt seem to work and I am getting blank graph.我尝试根据D的值将数据plot_ly sub setting为 3 个不同的数据框,并将表面添加到plot_ly函数,但它似乎不起作用,我得到了空白图形。 I don't know if I am using the right plot function.我不知道我是否使用了正确的绘图功能。

Below is my data frame d1下面是我的数据框d1

               A       B          C              D  
             734.5  2.28125     3.363312755      0    
             738    2.53125     3.395864326      0  
             727.25 2.484375    3.41183431       1
             737    2.421875    3.380499188      1
             727.25 2.3828125   3.39538442       1
             933.25 4.6875      3.148660474      1
             932.75 4.671875    3.155840809      1
             934    4.671875    3.165391107      1
             920.75 4.671875    3.194808475      1
             913.25 4.671875    3.22907393       1
             896.75 4.671875    3.287157844      1
             880    4.671875    3.341203642     -1
             866.75 4.59375     3.388017143     -1
             714.5  3.296875    3.572828317     -1
             730.75 3.296875    3.535364241     -1
             734.75 3.296875    3.526142314     -1
             713.25 3.7734375   3.653888449     -1
             711.75 3.8203125   3.665152882     -1
             711.75 3.8125      3.65967422      -1
             714    3.796875    3.630867839      0
             754.25 3.796875    3.560165628      0
             715.25 3.78125     3.650415301      0

Below is my R code下面是我的 R 代码

library(plotly)

pd1 <- subset(d1, (D == 1))
nd1 <- subset(d1, (D == -1))
zd1 <- subset(d1, (D == 0))  

p <-  plot_ly(showscale = FALSE) %>%
       add_surface(x= pd1$A,y = pd1$B, z = pd1$C)%>% 
        add_surface(x= nd1$A,y =nd1$B, z = nd1$C)%>% 
       add_surface(x= zd1$A,y =zd1$B, z = zd1$C)%>%

I think you have to pass a numeric matrix as argument to add_surface .我认为您必须将数字矩阵作为参数传递给add_surface

pd1_ma <- as.matrix(pd1)
nd1_ma <- as.matrix(nd1)
zd1_ma <- as.matrix(zd1)


p <- plot_ly(showscale = FALSE) %>%
  add_surface(z = ~pd1_ma) %>%
  add_surface(z = ~nd1_ma, opacity = 0.98) %>%
  add_surface(z = ~zd1_ma, opacity = 0.98)
p

That was working for me.那对我有用。

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

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