R Markdown

In this section we will focus on 3D scatter plots. In contrast with the 3D function plots presented above, you do not need to have data for all the combination of the X and Y axis. As an example, we will use the oribatid data given is in the ade4 package. The function to plot 3D plots is called scatterplot3d and it is found in the package scatterplot3d (which contains only one function… guess which one!).

library(ade4)
data(oribatid)
dens <- oribatid$envir$density
water <- oribatid$envir$water
sp <- oribatid$fau[,1]

Now it is possible to plot these three variables. With scatterplot3d(), a number of arguments were added to show a little bit of what this function can do:

library(scatterplot3d)
plot3D <- scatterplot3d(dens,water,sp,highlight.3d = TRUE, pch = 19, type = "h", xlab = "Soil density", ylab = "Soil water content", zlab = "species")

Notice that the result of scatterplot3d() was placed in the object plot3D. This was done because scatterplot3d() is a special function that include four subfunctions, which allow to add additional pieces to the graph previously drawn. For example, if we want to draw the result of a linear regression on this figure, which would be a plane (not a line), we can do it following this procedure. First we need to compute the linear regression between the species and the two explanatory variables:

reg <- lm(sp ~ dens + water)

Then we can draw the result of the regression on our 3D plot.

plot3D <- scatterplot3d(dens,water,sp,highlight.3d = TRUE, pch = 19, type = "h", xlab = "Soil density", ylab = "Soil water content", zlab = "species")
plot3D$plane3d(reg, col = "blue", lty.box = "solid")

Although this give an informative result, the graph may be easier to read if it was presented in a different angle. To do this you need to use the argument angle in the scatterplot3d() function.

plot3D.2 <- scatterplot3d(dens,water,sp,highlight.3d = TRUE, pch = 19, type = "h", xlab = "Soil density", ylab = "Soil water content", zlab = "species",angle = 145)

plot3D.2$plane3d(reg,col = "blue",lty.box = "solid")