[R] 4D Plot ??
Mike Prager
Mike.Prager at noaa.gov
Thu Apr 7 15:47:29 CEST 2005
Tried to post this last night, but it doesn't seem to have appeared.
Using R 2.0.1 on Windows XP + SP2.
I am traveling, away from my usual references. I'm trying to make a
4-dimensional plot: a levelplot with overlaid contours, with different
response variables represented by (1) colors on the levelplot and (2)
the contour lines.
First try was filled.contour + contour but the key printed by the first
means that the scales differ.
Then I tried levelplot. I couldn't figure out how to pass > 3 variables
to levelplot, so I duplicated all rows of the data frame and changed the
z data for the second half, in order to plot one half at a time.
#----------------------------------------------------------
# Try at a 4D contourplot:
y = x = 1:50
grid <- expand.grid(x=x, y=y)
grid$z = sqrt(x*y)
n1 = nrow(grid)
grid2 = rbind(grid,grid)
grid2$z[(n1+1):(n1*2)] = log(grid2$x[1:n1] * grid2$y[1:n1] + 10)
panel.4d <- function(x,y,z,subscripts) {
n1 = 1; n2 = length(x)/2
panel.levelplot(x[n1:n2],y[n1:n2],z[n1:n2],subscripts,region=TRUE)
n1 = n2 + 1 ; n2 = length(x)
panel.levelplot(x[n1:n2],y[n1:n2],z[n1:n2],subscripts,region=FALSE,contour=TRUE)
}
aa = levelplot(z~x*y, data=grid2, cuts = 20, panel=panel.4d)
print(aa)
#------------------------------------------------------------
This gives the following error message:
Error in panel.levelplot(x[n1:n2], y[n1:n2], z[n1:n2], subscripts,
region = FALSE, :
NAs are not allowed in subscripted assignments
although that it completes when the second levelplot is set to
region=TRUE, contour=FALSE (though then the second plot then hides the
first).
Hints or sample code will be most welcome. Once this works, the next
refinement will be to replace the colored levelplot with something
similar but with smooth edges produced by contouring, so advice on that
is also welcome.
Michael Prager, Ph.D.
NOAA Center for Coastal Fisheries & Habitat Research
Beaufort, North Carolina, USA
More information about the R-help
mailing list