r plotly 3d曲面图问题 [英] r plotly 3d surface plot issue

查看:73
本文介绍了r plotly 3d曲面图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下 plotly示例绘制3d表面图.

I am trying to plot a 3d surface plot based on these plotly examples

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

When I try these examples on my dataset

 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"

我能做的最好的事情就是这样

The best I could do was something like this below

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

但是这很混乱,没有像可绘制的曲面图示例那样的东西.

But this is messy and nothing like the plotly surface plot examples.

不确定我缺少什么,非常感谢您解决此问题并生成清晰的3d表面图.

Not sure what I am missing, any help on fixing this problem and producing a clean 3d surface plot is much appreciated.

推荐答案

add_surface要求xy形成网格,而z成为该网格上的矩阵. 在不确切知道您的数据是什么以及您想做什么的情况下,我只能猜测该怎么做:

add_surface requires x and y to form a grid and z to be a matrix over that grid. Without knowing exactly what your data are and what you want to do, I can only guess how to do that:

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

将为您提供

这篇关于r plotly 3d曲面图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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