Home > OS >  Lattice wireframe not creating 3d surface plot
Lattice wireframe not creating 3d surface plot

Time:10-23

I am back with another objectively simple question that has still managed to cause me much suffering. My literal only goal here is to use Wireframe to make a 3d surface plot with a polynomial smoother of the following formula: diamonds$price ~ diamonds$depth * diamonds$carat. The dataset being used is the built-in dataset 'diamonds' from ggplot2. For some reason my plot however looks very odd, as you can see. It looks more like a series of vertical lines than a nice 3d figure.

This is my code.

wf <- wireframe(price ~ carat * depth, data = diamonds,
                 shade = TRUE,
                zlab = "Price",
                xlab = "Depth",
                ylab = "Carat")
wf 

I tried changing the aspect ratio and that only made it worse, and I tried sequestering the variables... it just made my program crash. I know this is probably very simple to the more experienced coders out there, so I am genuinely very grateful to anyone who could offer some guidance.

Here is my unfortunate resulting plot: enter image description here

Thank you so much in advance, sincerely.

CodePudding user response:

Here is one way to get the plot. The diamonds data in ggplot2 is very large, 53,940 observations, so it will help to reduce it:

library(ggplot2)
data(diamonds)
library(lattice)
range(diamonds$carat)
# [1] 0.20 5.01
range(diamonds$depth)
# [1] 43 79
carat <- seq(.2, 5.01, length.out=100)
depth <- seq(43, 79, length.out=100)
g <- expand.grid(depth=depth, carat=carat)

We will create a smooth surface with a grid of 100 x 100 points for carat and depth. Now we need to estimate the price and then plot:

reg <- lm(price~depth*carat, diamonds)
g$price <- predict(reg, g)
wf <- wireframe(price~depth*carat, g, shade=TRUE, zlab = "Price",
      xlab = "Depth", ylab = "Carat")
wf

There are problems with the fit using depth and carat to predict price. The range of the predicted values is much greater than the actual range of diamonds$price so a better regression model would be preferable, but this illustrates the process for creating the plot.

Wireframe Plot

  • Related