Skip to content

Contour plots of four two-dimensional matrices

4 messages · Thomas Levine, David Winsemius

#
What is it that you want to do with these 4 plots? Overlay them with  
different color contours or plot them side-by-side on the same page?

?par  # for filled.contour but the implementation will be different  
for those two options.

  contourplot is is a lattice plotting function. See Figure 6.10 on  
Sarkar's Lattice book pages. levelplot is the closest analog to filled  
contour in lattice.
#
You would use layout to set up the page in base graphics. It sets up  
the page to receive multiple plots. Unfortunately, this will *not*  
give you side by side plots because filled.contour is restricted to a  
full page per its help page

layout(matrix(c(1,2,3,4), 2,2 byrow=TRUE)
for (i in 1:4) {
filled.contour(seven[ , , i] }


Lattice graphics looks to be your only option:

levelplot( in package lattice) has methods for arrays. This is what  
its help page says:
"Both levelplot and wireframe have methods for matrix, array, and  
table objects, in which case x provides the z vector described above,  
while its rows and columns are interpreted as the x and y vectors  
respectively. This is similar to the form used in filled.contour and  
image. For higher-dimensional arrays and tables, further dimensions  
are used as conditioning variables. "

Note that the matrix type is limited to 2 dimensions and you would  
need to use an "array" rather than a matrix. I just tested contourplot  
with the "three" example below and got encouraging results as well, so  
I think you are in luck. I would try simply this:

library(lattice)
contourplot(seven)   # can it really be this simple ?!?!

So your your data arrangement is in accord with that description. The  
desired 2 x 2 plot might happen automagically with your third  
dimension of the array = 4. The other more typical way to do it would  
be with a dataframe object that had x,y,z and grouping variables and  
to specify a formula like z ~ x + y | group. There is an example in  
the help page.

To that form with as.data.frame.table. Run this demo:

three <- array(1:27, c(3,3,3))
three
three.long <- as.data.frame.table(three)  # would need to relabel  
variable names
names(three.long) <- c("row", "col", instance", "Z")

HTH;
David Winsemius
On Mar 15, 2009, at 2:15 PM, Thomas Levine wrote:

            
David Winsemius, MD
Heritage Laboratories
West Hartford, CT