Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

r plotly 3d surface plot issue

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

I get an error

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.

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

will give you surface plot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...