r plotly 不产生表面图

r plotly not producing surface plot

我是一个完全的新手,非常感谢您的帮助!

试图创建一个表面 3D 图来绘制交互,但它没有显示该图(当我正在做一个 scatter3D 图时,它确实有效)。 使用了一个数据框,我希望我的 x 和 y 成为第 1 列和第 2 列,而我的 z(模型中 y 的预测值)成为第 3 列。

试图分别定义每个轴,没有任何区别):

df = data.frame(hobby = hobby,work = work, pred_well = predict.lm(m5.int)) 
plot(df)

plot_ly( df, type ="surface")

最终找到了它起作用的原因,并将其发布,因为我看到很多人都在为此苦苦挣扎

library(plotly) 
library(reshape2)

#Graph Resolution
graph_reso = 0.05

#Setup Axis
axis_x = seq(min(hobby), max(hobby), by = graph_reso)
axis_y = seq(min(work), max(work), by = graph_reso)

#Sample points
lm_surface = expand.grid(hobby = axis_x,work = axis_y,KEEP.OUT.ATTRS = F)
lm_surface$wellness = predict.lm(lm(wellness ~ work*hobby), newdata = lm_surface)
lm_surface = acast(lm_surface, work ~ hobby, value.var = "wellness") 

p_1  = plot_ly(myData, x = ~hobby, y = ~work, z = ~wellness, type = "scatter3d", mode = "markers" )%>% layout(title="Interaction Effect(Work Hours* Hobby Hours) on Wellness")

p_1 = add_trace(p = p_1,
                     z = lm_surface,
                     x = axis_x,
                     y = axis_y,
                     type = "surface")