Skip to contents

Add a 3 dimensional regression surface layer to a plot_ly object.

Usage

add_3d_surface(
  p,
  model,
  data = NULL,
  ci = TRUE,
  surfacecolor = "blue",
  surfacecolor_ci = "grey",
  opacity = 0.5,
  ...
)

Arguments

p

A plotly object.

model

An lm or glm with exactly two x variables

data

An optional dataframe to be used to estimate the regression surface. By default, this will be the data used by the inherited plotly object.

ci

An optional logical. Defaults to TRUE, showing the confidence intervals of the predicted effects.

surfacecolor

A color recognized by plotly. Used within the colorscale parameter in add_trace. Defaults to 'blue'.

surfacecolor_ci

A color recognized by plotly. Used within the colorscale parameter in add_trace. Defaults to 'grey'.

opacity

Sets the opacity of the surface. Defaults to 0.5.

...

Arguments (i.e., attributes) passed along to the trace type. See schema() for a list of acceptable attributes for a given trace type (by going to traces -> type -> attributes). Note that attributes provided at this level may override other arguments (e.g. plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))).

Value

A plotly object with the regression surface added to the plot.

Details

Note that the data used to estimate the regression surface in model must be the same as the data called in plot_ly or specified by the argument data.

Additional plotly layers such as add_markers() can be added to the plotly plot, but be aware that many plotly layers inherit the data from the prior layer. As such, a function such as add_markers() may not work as intended if called after add_3d_surface().

The surface can be built from either an lm or glm. For glms, testing has been primarily focused on binomial and Gamma families.

Examples

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: ‘plotly’
#> The following object is masked from ‘package:ggplot2’:
#> 
#>     last_plot
#> The following object is masked from ‘package:stats’:
#> 
#>     filter
#> The following object is masked from ‘package:graphics’:
#> 
#>     layout
mymodel <- lm(length ~ isFemale_num + isMale_num, data = hair_data)
p1 <- plot_ly(data = hair_data,
              x = ~isFemale_num,
              y = ~isMale_num,
              z = ~length )
add_3d_surface(p1, model = mymodel, data = hair_data)