Illogical Values Appearing in Graphs

I am using the R programming language. I made the following 3D plot in R:

# set seed for reproducibility 
#load libraries
    set.seed(123)
    library(dplyr)
    library(plotly)
    
#create data
    n <- 3
    my_grid <- expand.grid(i1 = 1:n, i2 = 1:n)
    my_grid$final_value = with(my_grid, sin(i1) + cos(i2) )
    
 
#make plot
       plot_ly() %>% 
        add_trace(data = my_grid,  x=my_grid$i1, y=my_grid$i2, z=my_grid$final_value, type='mesh3d') %>%
        add_surface(
            z = my_grid %>% as.matrix(),
            surfacecolor = my_grid,
            cauto=F,
            cmax=max(my_grid$final_value),
            cmin=min(my_grid$final_value)
        )

This produces the following plot:

enter image description here

As expected, this plot seems to be very logical : it shows a 3D surface where x = i1, y = i2, z = final_value, and the color of the plot is according "final_value".

Problem: If I try to add some more data to the grid and then create the plot:

#create more data
n <- 50
my_grid <- expand.grid(i1 = 1:n, i2 = 1:n)
my_grid$final_value = with(my_grid, sin(i1) + cos(i2) )


#make plot
plot_ly() %>% 
    add_trace(data = my_grid,  x=my_grid$i1, y=my_grid$i2, z=my_grid$final_value, type='mesh3d') %>%
    add_surface(
        z = my_grid %>% as.matrix(),
        surfacecolor = my_grid,
        cauto=F,
        cmax=max(my_grid$final_value),
        cmin=min(my_grid$final_value)
    )

enter image description here

Not only does this graph look "strange", but the "y-coordinate" (1697) within the hover text is displaying a value which is not present within the original data:

#display histogram of values

par(mfrow=c(1,3))

 hist(my_grid$i1)
 hist(my_grid$i2)
 hist(my_grid$final_value)

enter image description here

Question: In the above histogram, a value of "1697" does not appear in any of the variables. So how is it possible that such a large value is displayed within the plotly diagram?

Thanks

It looks like the y-limits are also wrong on the first plot? Note they go to about 9, not 3. So, I think the issue may be with you plot_ly call and that it's using the row #s. Is this closer:

library(dplyr)
library(plotly)

n <- 50
my_grid <- expand.grid(i1 = 1:n, i2 = 1:n)
my_grid$final_value = with(my_grid, sin(i1) + cos(i2) )

z = my_grid %>% as.matrix()

#dim(my_grid)
#tail(my_grid)

my_grid %>% plot_ly(x=my_grid$i1, y=my_grid$i2, z=my_grid$final_value) %>% add_trace(type='mesh3d') %>% add_surface(z = my_grid %>% as.matrix())

My guess is that the add_surface() call isn't quite right yet, particularly the z- function...

1 Like

Is this what you're trying to do:

my_grid<- matrix(nrow=n, ncol=n)
for(i in 1:n) for (j in 1:n) my_grid[i,j] <- sin(i)+cos(j)
dim(my_grid)
plot_ly(z=~my_grid) %>% 
  add_surface()

There's probably a better way to create the data then the nested for loops, but I think is how plot_ly() and add_surface() are expecting the data.

Thank you so much for your reply!

I tried to re-run your code and noticed the following:

https://imgur.com/a/RWh0Cvp

There still seems to be a point (1, 21, 49) on this graph which does not appear in the original data:

#display histogram of values

par(mfrow=c(1,3))

hist(my_grid$i1)
hist(my_grid$i2)
hist(my_grid$final_value)

Do you have any ideas why this might be happening?
Thanks for all your help!

And here it is again with a better solution than the nested for loops:

fun1 <- function(x,y){sin(x)+cos(y)}
n <- 50
my_grid <- outer(1:n,1:n,fun1)
plot_ly(z=~my_grid) %>% 
  add_surface()
1 Like

Thank you for your reply!

But why is this point still appearing on the graph that is not in the data frame?

Thanks for all your help!

I think this latest version doesn't have that other point?

fun1 <- function(x,y){sin(x)+cos(y)}
my_grid <- outer(1:50,1:50,fun1)
plot_ly(z=~my_grid) %>% add_trace(type='mesh3d') %>% 
  add_surface()

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.