在Plotly中配置添加到3d散点图的回归平面

时间:2019-09-12 14:04:13

标签: r ggplot2 plot plotly

我用图创建了一个3D散点图,并为数据的一个子集建模了回归平面,最后将其添加到所有点以实现更好的可视化。 如何更改添加的回归平面的色阶,以使其(i)没有(仅黑平面),或(ii)显示模型在空间给定点处的标准误差?

我有一个dataset mdata2的4个变量,我必须在第4维的3d(WindowUncvariable)中进行绘制是value。 我创建了一个subset of the data mdata3,并在其上建模了线性回归平面,将该平面扩展到整个数据集的维度。

per_lm <- lm(value ~ 0 + Unc + Window,data = mdata3)

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

#Setup Axis
axis_x <- seq(min(mdata2$Window), max(mdata2$Window), by = graph_reso)
axis_y <- seq(min(mdata2$Unc), max(mdata2$Unc), by = graph_reso)

#Sample points
per_lm_surface <- expand.grid(Window = axis_x,Unc = axis_y,KEEP.OUT.ATTRS = F)
per_lm_surface$value <- predict.lm(per_lm, newdata = per_lm_surface)
per_lm_surface <- acast(per_lm_surface, Unc ~ Window, value.var = "value") #y ~ x

我用plotly在3D中绘制了整个数据集,并向其中添加了回归平面。

library(reshape)
library(plotly)

 fivepercent <- plot_ly(mdata2, x = ~Window, y = ~Unc, z = ~variable,
             marker = list(color=~value, colorscale = "RdBu", showscale = TRUE)) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'Window'),
                      yaxis = list(title = 'Age uncertainty (yr)'),
                      zaxis = list(title = 'Period')),
                    annotations = list(
                    x = 1.13,
                    y = 1.05,
                    text = 'Spectral bias (yr)',
                    xref = 'paper',
                    yref = 'paper',
                    showarrow = FALSE))


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

fivepercent

并在link

上看到了情节

hereenter image description here

我本来希望看到显示的回归平面没有任何颜色,特别是因为它也弄乱了3d散点图的比例。

此外,关于散点图,是否有可能像this 2D ggplot plot enter image description here中那样在色标上造成断裂, 上面显示的只是3D图的一个周期切片?

谢谢您的帮助。

注:以下警告是由plotly

产生的
Warning message:
'surface' objects don't have these attributes: 'marker'
Valid attributes include:
'type', 'visible', 'showlegend', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'uirevision', 'z', 'x', 'y', 'text', 'hovertext', 'hovertemplate', 'connectgaps', 'surfacecolor', 'cauto', 'cmin', 'cmax', 'cmid', 'colorscale', 
'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'contours', 'hidesurface', 'lightposition', 'lighting', 'opacity', '_deprecated', 'hoverinfo', 'xcalendar', 'ycalendar', 'zcalendar', 'scene', 'idssrc', 'customdatasrc', 'zsrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'surfacecolorsrc', 'hoverinfosrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

0 个答案:

没有答案