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

From: Charilaos Skiadas <cskiadas_at_gmail.com>
Date: Thu, 15 May 2008 06:44:04 -0400

On May 15, 2008, at 3:18 AM, RINNER Heinrich wrote:

> Thanks for your help!
> I guess I could have thought for ages about this, and never would
> such a solution have come to my mind ;-)
> It works as far as the text in the strips is left-aligned; a
> remaining drawback is that printing of longer texts will be
> continued outside the right border of their strip.

Yes, the only way to fix that I think will likely be to tell it to clip the string off, and to do that you would likely want to copy some of what strip.default does with viewports. Effectively, you need to create a new viewport occupying the strip space, and tell it to use clipping there. See the code for strip.default, and the pushViewport and upViewport calls. For example, using that code, this might work:

require(grid)
strip.left.aligned <- function(which.given, which.panel, factor.levels, ...) {

   pushViewport(viewport(y = (which.given - 0.5)/length(which.panel),

         height = 1/length(which.panel), clip = trellis.par.get ("clip")$strip,

         name = paste("strip.default", which.given, sep = ".")))     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]])

    upViewport()
}
xyplot(y ~ x | a, data = test, strip = strip.left.aligned)

Haris Skiadas
Department of Mathematics and Computer Science Hanover College

> For example, using your suggestion:
>
> ###
> library(lattice)
> test <- data.frame(x=rnorm(100), y=rnorm(100), a=rep(c("A", "B",
> "C", "D: left-aligned text (possibly
> looooooooooooooooooooooooooooooooooooooooong string"),25))
>
> strip.left.aligned <- 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]])
> }
>
> xyplot(y ~ x | a, data = test, strip = strip.left.aligned)
> ###
>
> Anywag, thanks again for having taken the time thinking about my
> question;
> Heinrich.
>
>> -----Ursprüngliche Nachricht-----
>> Von: Charilaos Skiadas [mailto:cskiadas_at_gmail.com]
>> Gesendet: Mittwoch, 14. Mai 2008 13:30
>> An: RINNER Heinrich
>> Cc: r-help_at_stat.math.ethz.ch
>> Betreff: Re: [R] lattice: left-aligned text in strips?
>>
>>
>>
>> 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
>>
>>
>>
>>
>>



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. Received on Thu 15 May 2008 - 10:53:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 15 May 2008 - 11:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive