Learn R Programming

moderndive (version 0.8.0)

plot_3d_regression: Interactive 3D scatterplot with a fitted regression plane

Description

Build an interactive 3D scatterplot of an outcome against two numeric predictors, overlaid with the fitted plane from lm(formula, data). Returns a plotly htmlwidget that you can render in an R Markdown / Quarto document or in the RStudio Viewer.

Usage

plot_3d_regression(data, formula, n = 25)

Value

A plotly htmlwidget object.

Arguments

data

a data frame containing the three variables in formula.

formula

a formula of the form z ~ x + y: a single outcome on the left-hand side and exactly two predictor variables on the right-hand side. In-formula transformations (e.g. log(z) ~ x + y) are not supported in this version — apply transformations to data beforehand.

n

grid resolution per axis used to draw the regression plane. Default 25.

Details

Requires the suggested package plotly. Install it with install.packages("plotly") if you see an error about it being missing.

See Also

get_regression_table(), get_regression_points()

Examples

Run this code
if (FALSE) {
  library(moderndive)
  plot_3d_regression(
    un_member_states_2024,
    life_expectancy_2022 ~ gdp_per_capita + fertility_rate_2022
  )
}

Run the code above in your browser using DataLab