Re: [R] panel.axis() & grid/lattice settings

From: Paul Murrell <>
Date: Fri 03 Jun 2005 - 06:57:20 EST


Deepayan Sarkar wrote:
> On Wednesday 01 June 2005 05:43, Pikounis, Bill [CNTUS] wrote:

>>I wish to customize the tick marks and labels of axes in panels produced by
>>high-level lattice functions, namely xyplot. I know I can use the scales
>>argument to specify values for rot, cex, etc. in the top-level call.
>>However, I am interested in determining values for cex and rot based on the
>>current panel / viewport and device. More specifically, I would like to
>>make adjustments when tick labels overlap on the x-axis, such as labels of
>>a factor. If I use base graphics, par("cin") or par("cxy") or strwidth(),
>>etc. can be used to develop an algorithm to adjust cex or/and rot if
>>I am trying to determine the parameters/settings in grid analogous to
>>par("cin"), etc. mentioned above, knowing that par() has no effect in
>>lattice / grid. I have dug around the sources for grid and lattice but
>>cannot seem to come up with such parameters -- most notably something like
>>strwidth(). I see that panel.axis() has a check.overlap argument for
>>labels, but I could not trace down the actual code to see how that works.
>>What have I overlooked, or where should I be looking?

> Paul may be able to give a more insightful answer, but grid allows a string to
> determine it's width in terms of itself, e.g.:
> unit(1, "strwidth", data = "foo")

Or just stringWidth("foo") (and of course stringHeight("foo")).

> If you want to convert that into, say, inches, you could use

>>convertX(unit(1, "strwidth", data = "foo"), "inches", TRUE)

> [1] 0.2344092
> I think this would depend on the gpars() in effect, in particular fontsize.

Yep. There's also grobWidth(textGrob("foo")), which gives you not just the size of a string, but the size of a text graphical object. The difference is that a text graphical object can include information about where it will be drawn, in what font, and so on. For example, grobWidth(textGrob("foo", gp=gpar(fontfamily="mono", cex=2))) is very different from stringWidth("foo"). If you want to determine the size of some text that you want to draw as part of the panel axis AND you want it to look like the text that lattice would have drawn, then something like this might give you what you want:

grobWidth(textGrob("your label",

          "gpar",  trellis.par.get("axis.text"))))

It is also important that you do these calculations within a panel function so that they get evaluated within the appropriate context (i.e., higher-level lattice graphical parameter settings have been enforced). The result of the above call to grobWidth() could be very different if evaluated in some other context.

Finally, if you use convertX() (or any of its ilk), be aware that changes in the device size (e.g., resize a window) could make the calculations invalid. Maybe we could follow this up on the r-devel mailing list if you think this will be an issue.


>>Indirectly related, setting outside=TRUE in a panel.axis() call does not
>>produce visible labels, perhaps due to "issues of clipping" as mentioned in
>>its help page. How might one disable clipping for the current panel /

> At the grid level, there's a 'clip' argument to 'viewport()'. In lattice,
> these are chosen from

> List of 2
> $ panel: chr "on"
> $ strip: chr "on"
> (In case you are using 'trellis.focus', that can set clipping off.)
> Deepayan
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Fri Jun 03 07:03:32 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:22 EST