Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 285
King Frazier Avatar asked Sep 30 '16 04:09

King Frazier


People also ask

How do you plot 3 dimensional data in R?

Creating 3D Plots in R Programming – persp() Function 3D plot in R Language is used to add title, change viewing direction, and add color and shade to the plot. The persp() function which is used to create 3D surfaces in perspective view. This function will draw perspective plots of a surface over the x–y plane.

How do you explain a 3D surface plot?

A 3D surface plot is a three-dimensional graph that is useful for investigating desirable response values and operating conditions. A surface plot contains the following elements: Predictors on the x- and y-axes. A continuous surface that represents the response values on the z-axis.

What is the difference between contour plotting and surface response plotting?

In a contour plot, the response surface is viewed as a two-dimensional plane where all points that have the same response are connected to produce contour lines of constant responses. A surface plot generally displays a three-dimensional view that may provide a clearer picture of the response.

What is Z in Plotly?

Passing the arguments x, y, z suggests you want to display a scatter3d plot - you can test this by dropping add_surface() : z <- runif(50,0,1) y <- runif(50,1,2) x <- runif(50,3,6) plot_ly(x = x, y = y, z = z)


1 Answers

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

like image 143
mschilli Avatar answered Sep 24 '22 21:09

mschilli