Search code examples
rlatticewireframe

R lattice wireframe: how to increase the resolution of 3D graph


I have a question which I don't have the answer to after a while of research.

temp.df<-subset(spread.df, x<5 & x>1 & y>1 & y<5)

wireframe((temp.df$z ~ temp.df$x + temp.df$y),
  scales=list(arrows=F), 
  screen = list(z = 40,x= -60) 
)

If I run this code, the x and y axes are from 2 to 4 with only one increment in between, which is 3. This makes the graph very low res. Is there a way to bump the resolution without manipulating my original data set? By higher resolution, I mean subdividing the surface of the wireframe.

Thank you!


Solution

  • OK here is how you can interpolate the surface with the akima package. By default it will give you a 40x40 grid, based on the existing surface:

    require(akima)
    require(reshape2)
    
    temp.df<-expand.grid(x=2:4,y=2:4,z=0)
    temp.df$z<-rnorm(9,10,3)
    
    surface<-melt(interp(temp.df$x,temp.df$y,temp.df$z)) # melt() stretches out the surface to x,y,z as you've put into the original example
    flat<-surface[!is.na(surface$X1)&!is.na(surface$X2),] # drop the NAs
    
    #CONVERT SCALES BACK (INTERP GIVES YOU A 40x40 grid over the existing range)
    
    points<-data.frame(x=min(temp.df$x)+(flat$X1-1)/(40/diff(range(temp.df$x))),
                       y=min(temp.df$y)+(flat$X2-1)/(40/diff(range(temp.df$x))),
                       z=flat$value)
    
    wireframe((points$z ~ points$x + points$y),
              scales=list(arrows=F), 
              screen = list(z = 40,x= -60) 
    )
    

    enter image description here