Coloring Plots According to a 4th Variable

Suppose I have the following "data frame" ("my_grid"):

library(plotly)
library(dplyr)

#create grid and evaluate function
input_1 <- seq(0,100,1)
input_2 <- seq(0,100,1)
input_3 <- seq(0,100,1)
input_4 <- seq(0,100,1)

my_grid <- data.frame(input_1, input_2, input_3, input_4)
my_grid$final_value = sin(input_1) + cos(input_2) + input_3 + input_4

We can see how this data frame looks like:

head(my_grid)

  input_1 input_2 input_3 input_4 final_value
1       0       0       0       0    1.000000
2       1       1       1       1    3.381773
3       2       2       2       2    4.493151
4       3       3       3       3    5.151128
5       4       4       4       4    6.589554
6       5       5       5       5    9.324738

Question: I want to make a 3D surface plot with variables "input_1", "input_2", "input_3" - and then color the surface according to "final_value"

       plot_ly() %>% 
            add_trace(data = my_grid,  x=my_grid$input_1, y=my_grid$input_2, z=my_grid$input_3, type="mesh3d" )
 %>%   add_surface(surfacecolor = my_grid$final_value,
                  cauto=F,
                  cmax=max(my_grid$final_value),
                  cmin=min(my_grid$final_value)
      )

But this returns several errors, such as :

  • Error: unexpected SPECIAL in "%>%"
  • Error: unexpected ',' in " cauto=F,"

I have tried different ways to debug this code, but I can't seem to figure it out. Can someone please show me how to fix these errors?

Thanks

Here is an example of plotting three variables and coloring the surface with a fourth variable. note that the z values and the colors are in matrices.

X <- seq(0,3.1,0.1)
Y <- seq(0,3.1,0.1)
DF <- expand.grid(X,Y)

#Compute variable for colors
DF$Z <- sin(DF$Var1) + cos(DF$Var2)

#make a matrix of color values
Mat <- matrix(DF$Z,nrow = 32)

#make a matrix for z values, could be made with the same method as Mat2 if I
#had real data.
Mat2 <- matrix(rep(c(1:16,16:1),32),nrow=32)

plot_ly(y=~Y,x=X,  z=~Mat2) %>%  
  add_surface(surfacecolor=~Mat)
1 Like

Thank you so much for your answer! I really appreciate it!

I tried to modify your code - I wanted to make a grid of three variables X, Y and W - then plot these 3 variables and color them according to Z

X <- seq(0,3.1,0.1)
Y <- seq(0,3.1,0.1)
W <- seq(0,3.1,0.1)
DF <- expand.grid(X,Y, W)

#Compute variable for colors
DF$Z <- sin(DF$Var1) + cos(DF$Var2) + sin(DF$Var3)

#make a matrix of color values
Mat <- matrix(DF$Z,nrow = 32)

#make a matrix for z values, could be made with the same method as Mat2 if I
#had real data.
Mat2 <- matrix(rep(c(1:16,16:1),32),nrow=32)

plot_ly(y=~Y,x=X,  z=~W) %>%  
  add_surface(surfacecolor=~Mat)

But this results in an error:
Error: z must be a numeric matrix

Can you please show me how to fix this?

Thank you so much for all your help!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.