[R] Lattice strip labels for two factors

Gabor Grothendieck ggrothendieck at gmail.com
Sat Sep 23 16:02:59 CEST 2006


1. You can write a custom strip function:

	my.strip <- function(which.given, ..., factor.levels) {
	   levs <- if (which.given == 1) factor.levels
		   else c("faro", "porto", "lisbon", "setubal")
	   strip.default(which.given, ..., factor.levels = levs)
	}

	xyplot(value ~ year | fact1 * fact2, data = df,
	   strip = my.strip)

2. however, its probably easier just to change the levels in
the data frame.  Just do it in a copy if you don't want to
change the original one:

	df2 <- df
	levels(df2$fact2) <- c("faro", "porto", "lisbon", "setubal")
	xyplot(value ~ year | fact1 * fact2, data = df2)

3. or you can even do it inline in the data statement which
similarly won't change the original data frame:

	levs <- c("faro", "porto", "lisbon", "setubal")
	xyplot(value ~ year | fact1 * fact2,
	   data = replace(df, "fact2", structure(df$fact2, levels = levs)))
	head(df) # unchanged





On 9/23/06, Rafael Duarte <rduarte at ipimar.pt> wrote:
> Thank you for your suggestion.
> This could be a solution that I didn't think of.
>
> But I forgot to say that I didn't want to change the original data frame
> (I have other code that depends on the original df and on the original
> factor levels).
> I was looking more for an implementation directly in the xyplot call
> (same as I did for one factor). Is it possible/simple to do?
>
> Thank you,
> Rafael
>
>
> Gabor Grothendieck wrote:
>
> > Try this:
> >
> > levels(df$fact2) <- c("faro","porto","lisbon","setubal")
> > xyplot( value ~ year | fact1*fact2, data=df, type="b")
> >
> >
> > On 9/22/06, Rafael Duarte <rduarte at ipimar.pt> wrote:
> >
> >> Dear list,
> >> My problem is to change the strip text of lattice panels when using two
> >> factors.
> >> I have a data frame with two factors:
> >>
> >> df <- expand.grid( "fact1"=c("y","b","r"),
> >> "fact2"=c("far","por","lis","set"), "year"=1991:2000, "value"= NA)
> >> df[,"value"] <- sample(1:50, 120, replace=TRUE)
> >>
> >> I can make simple xyplot and change the text of the factor levels with
> >> strip.custom:
> >>
> >> require("lattice")
> >> xyplot( value ~ year | fact1, data=df, type="b", subset= fact2=="far",
> >> strip = strip.custom(bg=gray.colors(1,0.95), factor.levels=c("yellow",
> >> "black", "red")),
> >> layout=c(1,3)
> >> )
> >>
> >> But how can I change the text of the factor levels when using both
> >> factors as in this plot:
> >> xyplot( value ~ year | fact1*fact2, data=df, type="b")
> >>
> >> (fact2 levels text should change to:
> >> c("faro","porto","lisbon","setubal"))
> >>
> >> I read the help for strip.default and the emails archive, tried with
> >> "which.given" but could not find out how to accomplish this.
> >>
> >> Many thanks,
> >> Rafael Duarte
> >>
> >> --
> >> Rafael Duarte
> >> Marine Resources Department - DRM
> >> IPIMAR -  National Research Institute for Agriculture and Fisheries
> >> Av. Brasília, 1449-006 Lisbon  -  Portugal
> >> Tel:+351 21 302 7000      Fax:+351 21 301 5948
> >> e-mail: rduarte at ipimar.pt
> >>
> >> ______________________________________________
> >> 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
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
>
>
> --
> Rafael Duarte
> Marine Resources Department - DRM
> IPIMAR -  National Research Institute for Agriculture and Fisheries
> Av. Brasília, 1449-006 Lisbon  -  Portugal
> Tel:+351 21 302 7000      Fax:+351 21 301 5948
> e-mail: rduarte at ipimar.pt
>
>



More information about the R-help mailing list