[R] Contour plots of four two-dimensional matrices

David Winsemius dwinsemius at comcast.net
Sun Mar 15 20:08:44 CET 2009


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:

> I want to plot them side by side.
>
> On Sun, Mar 15, 2009 at 12:41 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
> 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.
> -- 
> David Winsemius
>
>
>
> On Mar 15, 2009, at 12:22 PM, Thomas Levine wrote:
>
> I have four large two-dimensional matrices of which I want to create  
> contour
> plots. Something like
>
> filled.contour(<matrix>)
> contourplot(<matrix>)
>
> works but only gives me one plot at a time. If I combine the four  
> matrices
> into one three-dimensional matrix, which I'll name "seven", there  
> should be
> a way of doing something like this
>
> contourplot(seven[,,k] for k in 1 to 4)
>
> such that they come out as one plot rather than four. I couldn't  
> figure out
> how to do this, so I tried a disgusting alternative that involved  
> generating
> x,y and k vectors, but I'd rather do it properly.
>
> Tom
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list