r plotly 3d surface plot问题

时间:2016-09-30 04:25:47

标签: r 3d visualization plotly surface

我正在尝试根据这些plotly examples

绘制三维表面图

当我在我的数据集上尝试这些示例时

 test_plotly =   structure(list(Age = c(82L, 82L, 83L, 83L, 83L, 81L,  
       81L, 
    81L, 79L, 80L, 82L, 78L, 78L, 79L, 78L, 80L, 79L, 77L, 77L, 77L, 
    77L, 78L, 76L, 77L, 77L, 78L, 77L, 76L, 83L, 79L, 76L, 84L, 75L, 
    75L, 77L, 74L, 74L, 75L, 74L, 74L, 73L, 73L, 74L, 81L, 84L, 73L, 
    72L, 73L, 71L, 71L, 73L, 72L, 79L, 72L, 71L, 76L, 72L, 75L, 73L, 
    71L, 70L, 79L, 69L, 70L, 70L, 70L, 77L, 69L, 69L, 68L, 69L, 73L, 
    69L, 69L, 74L, 68L, 69L, 70L, 74L, 68L, 68L, 68L, 68L, 68L, 68L, 
    80L, 69L, 72L, 80L, 80L, 81L, 81L, 67L, 66L, 67L, 78L, 77L, 67L, 
    67L, 81L), BMI = c(32.88, 30.79, 30.08, 30.08, 30.08, 30.08, 
    30.08, 30.08, 36.86, 36.86, 32.33, 43.17, 43.17, 26.89, 25.15, 
    24.59, 33.98, 27.83, 34.66, 33.81, 29.47, 23.54, 23.54, 30.29, 
    43.21, 33.03, 20.62, 41.41, 37.88, 25.58, 26.08, 31.18, 27.78, 
    23.35, 25.42, 27.75, 27.75, 28.83, 27.68, 27.14, 29.13, 27.23, 
    27.75, 25.25, 28.79, 34.46, 30.93, 22.57, 19.06, 27.81, 33.35, 
    28.63, 36.11, 37.94, 31.89, 31.23, 38.65, 23.39, 31.13, 31.13, 
    24.07, 27.01, 32.78, 37.82, 31.98, 28.77, 27.52, 32.88, 26.17, 
    46.24, 32.09, 32.78, 25.4, 32.72, 31.21, 29.13, 45.85, 27.99, 
    36.33, 32.21, 35.97, 28.88, 26.12, 26.31, 32.61, 32.08, 36.58, 
    31.53, 42.84, 42.84, 42.84, 42.84, 31.68, 54.27, 30.26, 29.33, 
    29.33, 31.53, 32.66, 36.82), Task_Completion_Time = c(25L, 
    19L, 25L, 38L, 38L, 25L, 38L, 38L, 16L, 16L, 32L, 39L, 49L, 33L, 
    9L, 20L, 29L, 25L, 46L, 27L, 24L, 24L, 24L, 18L, 52L, 23L, 20L, 
    31L, 45L, 27L, 24L, 10L, 18L, 77L, 40L, 26L, 10L, 37L, 39L, 21L, 
    11L, 26L, 23L, 39L, 31L, 13L, 44L, 20L, 40L, 30L, 24L, 11L, 30L, 
    28L, 34L, 26L, 53L, 16L, 10L, 10L, 22L, 15L, 32L, 50L, 52L, 18L, 
    27L, 21L, 36L, 23L, 17L, 20L, 34L, 27L, 47L, 38L, 47L, 53L, 48L, 
    21L, 15L, 19L, 45L, 20L, 11L, 35L, 27L, 17L, 30L, 88L, 30L, 88L, 
    18L, 41L, 24L, 25L, 25L, 40L, 19L, 16L)), .Names = c("Age", 
    "BMI", "Task_Completion_Time"), class = c("data.table", 
    "data.frame"), row.names = c(NA, -100L))

我收到错误

Error in function_list[[k]](value) : 
  could not find function "add_surface"

我能做的最好的事情就像下面这样

plot_ly(z = as.numeric(test_plotly$Age), y = as.numeric(test_plotly$Task_Completion_Time), x = as.numeric(test_plotly$BMI))

但这很乱,而且不像情节表面情节的例子。

enter image description here

不确定我错过了什么,解决此问题的任何帮助以及制作干净的3D表面图表都非常感谢。

1 个答案:

答案 0 :(得分:4)

add_surface要求xy形成一个网格,并z作为该网格上的矩阵。 如果不确切知道您的数据是什么以及您想做什么,我只能猜测如何做到这一点:

library(akima)

# interpolate data onto grid
test_plotly <- with(test_plotly, interp(Age, BMI, Task_Completion_Time,
                                        duplicate = "mean"))

# plot surface over grid
plot_ly(x = test_plotly$x, y = test_plotly$y, z = test_plotly$z,
        type = "surface")

会给你surface plot