Three-dimensional scatter plots with a regression plane

In this recipe, we will see how we can visualize a three-dimensional scatter plot with a fitted regression plane.

Let's recall the airquality data from the datasets library. In this particular recipe, we will use Ozone as a dependent variable and Solar.R and Temp as independent variables in order to fit the regression model, and then we visualized it.

To produce this plot, we will perform the following steps:

Here is the R code that performs the preceding steps:

How to do it…

The scatterplot3d function produces the basic scatter plot for the three input numeric variables. The fitted line (the plane, in this case) is then embedded with the scatter plot using the plane3d function.

We can now add a horizontal drop line with the existing plot using the type="h" argument. There are other arguments to write the customized axis labels and plot title, such as xlab, ylab, and main.