[R] muliple plots with pairs (matrix of scatter plots)

Paul Murrell p.murrell at auckland.ac.nz
Tue Dec 21 00:47:44 CET 2004


Uwe Ligges wrote:
> Tiago R Magalhaes wrote:
>> I am trying to make a graph with 4 scatter matrixes plots and couldn't 
>> do it. While trying to find a solution for this I also came across the 
>> idea of giving different values to the same argument for each of the 
>> lower and upper function but couldn't do it. (Examplified below with 
>> the col argument). The first problem of plotting 4 scatter matrixes in 
>> a graph is a problem of real interest for me at this point. The second 
>> problem is a matter of curiosity.
>> I am using a Mac PowerBook G4 with OS 10.3.7 and R 2.0.1
>> Problem 1)
>> x=data.frame(a=sample(1:100, 50), b=sample(1:100, 50),c=sample(1:100, 
>> 50),d=sample(1:100, 50))
>> x.list=vector('list',4)
>> for (j in 1:4) x.list[[j]]=x
>> #produces a graph with four plots:
>> layout(matrix(c(1,3,2,4),2,2))
>> for (j in seq(x)){
>>     plot(x.list[[j]][1:2])
>>     }
>> # But unfortunately the following produces a new plot everytime:
>> layout(matrix(c(1,3,2,4),2,2))
>> for (j in seq(x)){
>>     pairs(x.list[[j]])
>>     }
>> #Maybe pairs can't be used to produce a graph with multiple plots?
> Yes, it uses similar constructs to put multiple plots together.
> You might want to use packages grid and gridBase to set something up 
> using viewports.

I don't think that's going to work either -- pairs() makes some pretty 
strong assumptions that it is the only plot on the page.

One possible way to go is to use splom() instead from the lattice 
package.  For example (using your data from above) ...

splom(~ x)

... and lattice plots can be embedded in grid viewports easily, for 
example ...

pushViewport(viewport(layout=grid.layout(2, 2)))
for (j in seq(x)) {
   row <- (j - 1) %/% 2 + 1
   col <- (j - 1) %% 2 + 1
   print(splom(~ x.list[[j]]), newpage=FALSE)

... you may need to fiddle with the splom() args to get them looking how 
you want them.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz

More information about the R-help mailing list