Skip to content

4D Plot ??

3 messages · Mike Prager, Thomas Lumley, Hadley Wickham

#
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
#
On Thu, 7 Apr 2005, Mike Prager wrote:

            
You could put the contour() call in the plot.axes argument in the 
filled.contour() call.  This is a useful trick for getting the right 
scales for an overlay.

 	-thomas
#
Hi Mike,

I've done a bit of playing around with these kind of plots for
visualising microarray data (to eventually go into a bioconductor
package).  I've attached my code for producing surfaceplots (my  name
for the type of plots that includes both image and contour plots) -
it's all lattice based, so you'll need some familiarity with how
lattice works to understand how it all works.

The key function is panel.superpose.surface which you can use as follows:

levelplot(surfacevar + contourvar ~ x * y, data,
panel=panel.surface.smooth, asp="iso")

(note that the contours are automatically smoothed using image.smooth
from the fields package - you can control the amount of smoothing use
contour.theta)

You can supply multiple contour variables, but be advised it gets
messy really quickly!  You can also smooth the surface by setting
panel.base = panel.surface.smooth

Hope this is helpful!

Hadley
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: surfaceplot-ma.R
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050407/a6a230f1/surfaceplot-ma.pl