r中具有Plot_ly的3D曲面,具有x,y,z坐标 [英] 3D Surface with Plot_ly in r, with x,y,z coordinates

查看:61
本文介绍了r中具有Plot_ly的3D曲面,具有x,y,z坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用3D表面图来分析作为输入函数的结果的稳定性.z是我的结果,x和y是参数值.使用晶格的线框,我可以毫无问题地进行表面绘制,但是具有plotly的交互式和可共享功能将是很好的.火山示例未显示如何获取x,y,z坐标并绘制表面图.有人知道如何简洁地做到这一点吗?这是点阵线框示例和数据:

I am trying to use 3D surface plots to analyze the stability of outcomes as a function of inputs. z is my outcome, and x and y are parameter values. Using lattice's wireframe I can make a surface plot no problem, but it would be nice to have the interactive and sharable capabilities of plotly. The volcano example doesn't show how to take x,y,z coordinates and make a surface plot. Anyone know how to concisely do that? Here is the lattice wireframe example and data:

library(lattice) 

z <- c(0.035, 0.043, 0.036, 0.037, 0.016, 0.013, 0.025, 0.037, 0.039, 0.043,         
0.049, 0.029, 0.025, 0.02,  0.032, 0.045, 0.049, 0.043, 0.027, 0.031, 
0.028,0.039, 0.038, 0.037, 0.045, 0.046, 0.04,  0.041, 0.037, 0.048, 0.048, 
0.032, 0.03,  0.032, 0.029, 0.043, 0.056, 0.057, 0.046, 0.034, 0.035, 
0.026,0.036, 0.048, 0.038, 0.032, 0.034, 0.027, 0.024, 0.041, 0.05,  0.056, 
0.038, 0.027, 0.034, 0.027, 0.042, 0.054, 0.054, 0.039, 0.036, 0.029, 
0.027,0.04,  0.052, 0.058, 0.051, 0.028, 0.025, 0.035, 0.037, 0.053, 0.059, 
0.049, 0.044, 0.035, 0.021, 0.044, 0.059, 0.054, 0.052, 0.041, 0.032, 
0.036,0.039, 0.051, 0.056, 0.038, 0.04,  0.022, 0.015, 0.043, 0.054, 0.052, 
0.05,  0.031, 0.03,  0.03,  0.031, 0.044, 0.051, 0.047, 0.04,  0.038, 
0.037,0.06,  0.071, 0.07,  0.066, 0.038, 0.031, 0.045, 0.064, 0.065, 0.074, 
0.08,  0.052, 0.048, 0.041, 0.053, 0.072, 0.079, 0.071, 0.043, 0.059, 0.05, 
0.061, 0.061, 0.055, 0.069, 0.07,  0.057, 0.057, 0.055, 0.074, 0.071, 0.051, 
0.048, 0.05,  0.053, 0.069, 0.086, 0.085, 0.07,  0.047, 0.05,  0.047,0.055, 
0.073, 0.065, 0.056, 0.054, 0.048, 0.043, 0.063, 0.078, 0.087, 0.068, 0.047, 
0.052, 0.048, 0.065, 0.085, 0.084, 0.062, 0.049, 0.05,  0.05, 0.068, 0.084, 
0.095, 0.089, 0.052, 0.052, 0.062, 0.062, 0.086, 0.09,  0.078, 0.062, 0.058, 
0.038, 0.068, 0.095, 0.09,  0.082, 0.069, 0.057, 0.058,0.064, 0.082, 0.087, 
0.055, 0.053, 0.032, 0.033, 0.067, 0.087, 0.087, 0.079, 0.058, 0.053, 0.057, 
0.047, 0.069, 0.085, 0.075, 0.068, 0.063, 0.062,0.076, 0.089, 0.095, 0.086, 
0.056, 0.053, 0.063, 0.082, 0.08,  0.099, 0.101, 0.067, 0.065, 0.058, 0.066, 
0.086, 0.103, 0.088, 0.051, 0.072, 0.06, 0.08,  0.078, 0.067, 0.084, 0.086, 
0.067, 0.066, 0.068, 0.087, 0.087, 0.071, 0.063, 0.065, 0.067, 0.087, 0.104, 
0.108, 0.086, 0.057, 0.063, 0.056,0.064, 0.087, 0.084, 0.07,  0.063, 0.061, 
0.06,  0.076, 0.095, 0.11,  0.088, 0.057, 0.062, 0.067, 0.079, 0.097, 0.105, 
0.079, 0.055, 0.064, 0.066,0.087, 0.104, 0.12,  0.113, 0.063, 0.07,  0.075, 
0.073, 0.104, 0.111, 0.091, 0.072, 0.07,  0.05,  0.083, 0.114, 0.11,  0.098, 
0.083, 0.077, 0.07, 0.078, 0.098, 0.105, 0.069, 0.062, 0.045, 0.046, 0.083, 
0.106, 0.106, 0.093, 0.077, 0.075, 0.073, 0.057, 0.088, 0.105, 0.094, 0.084, 
0.082, 0.081)

x <- c(1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  
10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10,13, 16, 19, 1,  
4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 
19, 1,  4,  7,  10, 13, 16, 19, 1, 4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 
13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  
7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 
1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4, 7,  10, 13, 
16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  
10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  
4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 
19, 1,  4,  7, 10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 
13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19,1,  4,  
7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 
1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10,13, 16, 19, 1,  4,  7,  10, 13, 
16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  4,  7,  
10, 13, 16, 19, 1, 4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19, 1,  
4,  7,  10, 13, 16, 19, 1,  4,  7,  10, 13, 16, 19)

y  <- c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 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3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3,0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3)

wireframe(z ~ x * y, xlab="Parameter 1", ylab="Parameter 2", zlab="Fitness Value")

我想使用相似的方法,但是它总是说z需要是一个矩阵.而且我不明白这是如何映射到x和y的.

Using plotly I would like to do something similar, but it always says that z needs to be a matrix. And I don't understand how this maps to x and y.

library(plotly)
p <- plot_ly(x = x, y = y, z = z) %>% add_surface()
p

最好是一个data.table示例,因为我的数据实际上位于data.table中,其中z是列,x和y是列,还有许多其他参数列将用于情节.

Even better would be a data.table example, because my data is actually in a data.table where z is a column, x and y are columns, and there are a large number of other parameter columns that will be used for plots.

df.ex <- data.table(Output=z,Input1=x,Input2=y,Input3=rnorm(315),Input4 = 
abs(rnorm(315)+.5))

wireframe(df.ex$Output ~ df.ex$Input1 * df.ex$Input2)

推荐答案

类似的方法效果很好.这就是你想要的吗?

Something like this works well. Is this what you want?

library(plotly)
data <- data.frame(x=x,y=y,z=z)
plot_ly() %>% 
  add_trace(data = data,  x=data$x, y=data$y, z=data$z, type="mesh3d" ) 

这篇关于r中具有Plot_ly的3D曲面,具有x,y,z坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆