[R] lattice: left-aligned text in strips?

Charilaos Skiadas cskiadas at gmail.com
Wed May 14 13:29:43 CEST 2008


On May 14, 2008, at 3:47 AM, RINNER Heinrich wrote:

> [adapted repost of question
> http://tolstoy.newcastle.edu.au/R/e4/help/08/03/6260.html]
>
> Dear R community,
>
> by default, text in the strips of a trellis plot is centered in the
> strip.
> Is there a way to have the text left-aligned?
>
> For example:
>
> library(lattice)
> test <- data.frame(x=rnorm(100), y=rnorm(100), a=rep(c("A: centered
> text","B: centered text"),50))
> xyplot(y ~ x | a, data = test)  # ok, strip text is centered
> test <- data.frame(x=rnorm(100), y=rnorm(100), a=rep(c("A: left- 
> aligned
> text","B: left-aligned text (possibly loooooooooooooooooooooooooooong
> string"),50))
> xyplot(y ~ x | a, data = test)  # how???

Here's a way to do it, shamelessly stealing the main idea from:  
http://dsarkar.fhcrc.org/lattice/book/figures.html? 
chapter=10;figure=10_24

Mainly you just have to write your own strip function.

xyplot(Petal.Length~Petal.Width|Species, iris, strip=function 
(which.given,which.panel, factor.levels,...) {
   panel.rect(0, 0, 1, 1, col = trellis.par.get("strip.background") 
$col[which.given], border = 1)
   panel.text(x = 0, y = 0.5, pos = 4, lab = factor.levels[which.panel 
[which.given]])
} )

Better yet, you should probably write a more generic strip function  
that does what you want.

I would however consider using ?abbreviate for what you want to do  
instead.

> I am using R 2.6.2 on Windows XP, package lattice Version 0.17-6.
>
> [The reason I would like to do this is because "in real life" my
> conditioning variable 'a' can have quite long strings as its value.  
> I am
> automatically creating a series of trellis plots, and in each one a
> different number of panels will be produced (maybe 4, maybe  
> 20,...). So
> in some cases (few panels, short labels) text in the strips will be
> perfectly readable, while in some cases (many panels, long labels)  
> only
> the middle of the text will.
> I know I could abbreviate the strip text by using something like:
> xyplot(y ~ x | substr(a,1,35), data = test)
> But there is no "natural" choice of string length here when I want to
> cut off as few text as possible, so just left aligning the strip texts
> would seem like a natural and easy(?) solution to me - if I was  
> able to
> do it...]
>
> -Heinrich.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list