surfaceplot can't mapping 3D.Where am I going wrong?

I'm learning about plotly.

I tried 3D scatter + surface

this is iris try code. from stack overflow


library(plotly)
library(reshape2)

#load data

my_df <- iris
petal_lm <- lm(Petal.Length ~ 0 + Sepal.Length + Sepal.Width,data = my_df)

#Graph Resolution (more important for more complex shapes)
graph_reso <- 0.05

#Setup Axis
axis_x <- seq(min(my_df$Sepal.Length), max(my_df$Sepal.Length), by = graph_reso)
axis_y <- seq(min(my_df$Sepal.Width), max(my_df$Sepal.Width), by = graph_reso)

#Sample points
petal_lm_surface <- expand.grid(Sepal.Length = axis_x,Sepal.Width = axis_y,KEEP.OUT.ATTRS = F)
petal_lm_surface$Petal.Length <- predict.lm(petal_lm, newdata = petal_lm_surface)
petal_lm_surface <- acast(petal_lm_surface, Sepal.Width ~ Sepal.Length, value.var = "Petal.Length") #y ~ x

hcolors=c("red","blue","green")[iris$Species]
iris_plot <- plot_ly(my_df, 
                     x = ~Sepal.Length, 
                     y = ~Sepal.Width, 
                     z = ~Petal.Length,
                     text = ~Species, # EDIT: ~ added
                     type = "scatter3d", 
                     mode = "markers",
                     marker = list(color = hcolors))

iris_plot <- add_trace(p = iris_plot,
                       z = petal_lm_surface,
                       x = axis_x,
                       y = axis_y,
                       type = "surface")

iris_plot

I get an error code, but the point and surface are output simultaneously, which is the output I want.

The following code does not draw the surface.


library(tidyverse)
library(plotly)

plot_ly(diamonds %>% filter(color %in% c("J","D")), 
        x = ~carat, 
        y = ~depth, 
        z = ~price,
        text = ~color, 
        type = "scatter3d",
        color = ~color,
        mode = "markers",
        size=0.1,
        alpha = 0.2) 


model_j = lm(data = diamonds %>% filter(color %in% c("J")),
   price~carat+depth)

model_d = lm(data = diamonds %>% filter(color %in% c("D")),
   price~carat+depth)


axis_x <- seq(min(diamonds$carat), max(diamonds$carat), length=1000)
axis_y <- seq(min(diamonds$depth), max(diamonds$depth), length=1000)

pred_j = predict.lm(model_j, bind_cols(carat=axis_x,depth=axis_y))
pred_d = predict.lm(model_d, bind_cols(carat=axis_x,depth=axis_y))

plot_ly(diamonds %>% filter(color %in% c("J","D")), 
        x = ~carat, 
        y = ~depth, 
        z = ~price,
        text = ~color, 
        type = "scatter3d",
        color = ~color,
        mode = "markers",
        size=0.1,
        alpha = 0.2) %>% 
  add_trace(
            z = pred_j,
            x = axis_x,
            y = axis_y,
            type = "surface") %>% 
  add_trace(
    z = pred_d,
    x = axis_x,
    y = axis_y,
    type = "surface")

Where am I going wrong?

thank you.

It appears to when I run it, but the points are closely bunched, which is consisted with the fitted models.

1 Like

@technocrat

thanks your advice!!

how can I make surface? (I need lm surface...)

I can't see the surface.

P.S.

II found the mistake in my code.
It seems that I need to enter a matrix for the z in add_trace.

1 Like