[R] lattice multiple plots per page

Paul Murrell p.murrell at auckland.ac.nz
Wed Feb 16 03:19:57 CET 2005


Hi


Deepayan Sarkar wrote:
> On Monday 14 February 2005 18:12, Jeff Jorgensen wrote:
> 
>>Dear R-sters,
>>
>>I was wondering if anyone has encountered the following issues.  I've
>>figured out how to get multiple levelplots [library(lattice)] on a
>>single plot.  However, when I add text (adding axis labels for the
>>entire four panel plot) the text is missing when I insert the *.eps
>>file I've created into my LaTeX document (via MikTeX-WinEdt).  And,
>>I've just upgraded to R v2.0.1 from v1.8.1 (Windows), and each
>>individual levelplot is smaller compared to the older R release.
>>
>>Any clues as to 1) how I can recover the lost text, and 
> 
> 
> Your use is incorrect, in the sense that the return value of 
> grid.locator() is not a valid input of ltext. You could do the 
> following to fix this, 
> 
> library(grid)
> ltext(lapply(grid.locator(), convertX, "native", TRUE),
>       lab="x-axis label, where I click",cex=1.5)
> ltext(lapply(grid.locator(), convertX, "native", TRUE),
>       lab = "y-axis label, where I click",
>       cex=1.5, srt=90)
 > but that doesn't seem to help either. So, there seems to be a bug
 > somewhere.


I think this has the same problem as the original code, which is that it 
attempts to use ltext() outside of a lattice panel function, so the 
"native" units assumed by ltext() could be inappropriate.

Within the top-level grid viewport, "native" units are (at least 
approximately) the native units of the device (i.e., "pixels").  The 
original drawing on screen is occurring in terms of screen pixels, but 
when you do the dev.copy2eps(), "native" means something different and 
you end up drawing in terms of PostScript points (1/72 inches), so the 
locations may be different (especially if your screen is, say, 96 dpi).


 > However, you presumably want something like
 >
> grid.text(x = .05, y = .5, lab="y-axis label", 
>           default.units = "npc", gp = gpar(cex=1.5), 
>           rot = 90)
> grid.text(x = .5, y = .05, lab="x-axis label", 
>           default.units = "npc", gp = gpar(cex=1.5))
> 
> for the last two steps, which seems to work. 


Right.  First of all, use grid.text() not ltext(), and secondly,  use 
"npc" coordinates (in fact, anything other than "native" should do), 
which should transform to the same location on the dev.copy2eps().

If you want to position the labels using a mouse click ...

xy <- grid.locator("npc")
grid.text(xy$x, xy$y, lab="y-axis label",
           gp = gpar(cex=1.5), rot = 90)
xy <- grid.locator("npc")
grid.text(xy$x, xy$y, lab="x-axis label",
           gp = gpar(cex=1.5))

... again using "npc" locations rather than "native".

Paul


>>2) increase the size of each of the levelplots?
> 
> 
> You need to change the following settings, in particular the entries 
> that end with 'padding' (similarly for 'layout.heights').
> 
> 
>>str(trellis.par.get("layout.widths"))
> 
> List of 13
>  $ left.padding     : num 1
>  $ key.left         : num 1
>  $ key.ylab.padding : num 1
>  $ ylab             : num 1
>  $ ylab.axis.padding: num 1
>  $ axis.left        : num 1
>  $ axis.panel       : num 1
>  $ panel            : num 1
>  $ between          : num 1
>  $ axis.right       : num 1
>  $ axis.key.padding : num 1
>  $ key.right        : num 1
>  $ right.padding    : num 1
> 
> Deepayan
> 
> 
>>Cheers,
>>
>>Jeff Jorgensen
>>
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>Sample code that illustrates what I'm trying to do:
>>
>>#create a levelplot
>>x<-seq(pi/4, 5*pi, length = 100)
>>y<-seq(pi/4, 5*pi, length = 100)
>>r<-as.vector(sqrt(outer(x^2, y^2, "+")))
>>grid<-expand.grid(x=x, y=y)
>>grid$z<-cos(r^2) * exp(-r/(pi^3))
>>a<-levelplot(z~x*y, grid, cuts = 50, xlab="", ylab=",
>>  colorkey = FALSE)
>>
>>#create the multiple panel plot, here using all the same levelplot
>>trellis.par.set(list(background=list(col="white"))) #white background
>>#using position to scale the plots up and to the right ~10%
>>#to make room for the axis labels
>>print(a,position=c(0.1,0.1,1,1),split=c(1,1,2,2),more=T)
>>print(a,position=c(0.1,0.1,1,1),split=c(1,2,2,2),more=T)
>>print(a,position=c(0.1,0.1,1,1),split=c(2,1,2,2),more=T)
>>print(a,position=c(0.1,0.1,1,1),split=c(2,2,2,2),more=F)
>>#commands that let you click where you want the labels centered
>>ltext(grid::grid.locator(),lab="x-axis label, where I click",cex=1.5)
>>ltext(grid::grid.locator(),lab="y-axis label, where I
>>click",cex=1.5,srt=90) #save device to an *.eps file, to be called
>>later by a \includegraphics command
>>dev.copy2eps(file="twobytwoplot.eps")
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


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




More information about the R-help mailing list