[R] Adding regression surface to cloud plot

Deepayan Sarkar deepayan at stat.wisc.edu
Tue Apr 27 17:00:01 CEST 2004


On Monday 26 April 2004 08:14, Anne wrote:
> Hello!
> I would like to add a plot of the regression surface to my cloud plot
> . Is it possible? Thanks

Possible (since 1.9.0), but not easy. Technically, the biggest problem 
is deciding which points are 'behind' the surface and which 'in 
front' (that would decide the order of plotting). Otherwise, all the 
pieces are in place.

The following should work for regression surfaces, but may need an 
inequality to be changed for specific viewpoints:


panel.3dreg <-
    function(x, y, z, rot.mat, distance,
             xlim.scaled, ylim.scaled, 
             nmesh = 21, ...)
{
    fm <- lm(z ~ x + y)
    id <- predict(fm) < z ## may need to be reversed
    panel.3dscatter(x[id], y[id], z[id], rot.mat, distance,
                    xlim.scaled = xlim.scaled,
                    ylim.scaled = ylim.scaled,
                    ...)
    x.locs <- seq(xlim.scaled[1], xlim.scaled[2], length = nmesh)
    y.locs <- seq(ylim.scaled[1], ylim.scaled[2], length = nmesh)
    grid <-
        expand.grid(y = y.locs, x = x.locs)
    grid$z <- predict(fm, newdata = grid)
    panel.3dwire(x = x.locs, y = y.locs, z = grid$z,
                 rot.mat, distance,
                 xlim.scaled = xlim.scaled,
                 ylim.scaled = ylim.scaled,
                 ..., col.at = .5, col.regions = "transparent")
    panel.3dscatter(x[!id], y[!id], z[!id], rot.mat, distance,
                    xlim.scaled = xlim.scaled,
                    ylim.scaled = ylim.scaled,
                    ...)
}

## example
data(rock)
cloud(area ~ peri * shape, rock,
      panel.3d.cloud = panel.3dreg,
      nmesh = 40, pch = 16)


- Deepayan




More information about the R-help mailing list