[R] Specifying strip names in xyplot (strip.custom)

Duncan Mackay dulcalma at bigpond.com
Sun Jan 18 05:06:40 CET 2015


I think this is want you want but without an example I am not certain

dat = expand.grid(t1 = gl(3,4), t2 = gl(5,4))
dat$y = rnorm(240)
dat$x = 1:12

xyplot(y~x|t2*t1,dat, as.table = T)

my.strip <-
function(which.given, ..., factor.levels) {
         levs <- if (which.given == 2) c("Concord","HAL","Pierce") #levels
for second factor T2
                      else paste("T2 ", 1:5) #levels for first factor
           strip.default(which.given, ..., factor.levels = levs)
}
xyplot(y~x|t2*t1,dat,
       as.table = T,
       par.settings = list(add.text = list(cex = 0.7)),
       strip    = my.strip,
       layout = c(5,3))

The par.settings is a cheats way of changing the cex

# Change order
my.strip2 <-
function(which.given, ..., factor.levels) {
         levs <- if (which.given == 2) c("HAL","Concord","Pierce") #levels
for second factor T2
                      else paste("T2 ", 1:5) #levels for first factor
           strip.default(which.given, ..., factor.levels = levs)
}

xyplot(y~x|t2*factor(dat$t1, levels = c(2,1,3)),dat,
       as.table = T,
       par.settings = list(add.text = list(cex = 0.7)),
       strip    = my.strip2,
       layout = c(5,3))

or can reorder

Duncan

-----Original Message-----
From: Ahmed Attia [mailto:ahmedatia80 at gmail.com] 
Sent: Sunday, 18 January 2015 07:10
To: Duncan Mackay
Cc: R
Subject: Re: Specifying strip names in xyplot (strip.custom)

Hi

I attach the data and a xyplot I made using strip.default;


# This is my code;

trellis.device(col=F)
xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
layout=c(5,3,1),
xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
#xlab=expression(paste("Herbicide rate (g isoxaflutole ", ha^-1, ")")),
#ylab=expression(paste("Root dry weight (mg  ", pot^-1,")")),
ylab=expression(bold(paste("Grain yield (Mg  ",ha^-1,")"))),
        ylim=c(10,20), #xlim=c(-50,700),
  strip = function(...) strip.default(...,style = 1), col=0,
        par.strip.text = list(cex = 0.7),
        scales =
list(alternating=F,y=list(at=c(10,12,14,16,18,20,22)),font=2, cex=0.7,
col="black"),aspect = 1,
  panel = function(x,y,subscripts,groups,...) {
   #panel.grid(-1,-1)
         m1 <- lm(y ~ x)
         x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
         pred <- predict(m1, x.seq, se.fit=T)
         grid.polygon(c(x.seq$x,rev(x.seq$x)),
          c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
          gp=gpar(col=0, fill="light grey", alpha = 1), default.units
= "native")
   llines(x.seq$x, pred$fit, lty = 1)
         panel.xyplot(x,y)
         print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
         ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[
2,c(1,4) ],3),sep=""),
                                 cex=0.65, adj=0)
        ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[
1,c(1,4) ],3),sep=""),
                                 cex=0.65, adj=0)
                                 print(summary(m1))
    }
   )




## I want to force the model to write the names of locations as
"Concord","HAL","Pierce" from top to bottom instead of Loc.

Thanks for your help.

Ahmed
Ahmed Attia, Ph.D.
Agronomist & Soil Scientist

Post-Doc Research Associate
Texas A&M AgriLife Research-Vernon
ahmed.attia at ag.tamu.edu
Cell phone: 001-979-248-5215




On Sat, Jan 17, 2015 at 5:12 AM, Duncan Mackay <dulcalma at bigpond.com> wrote:
> Hi Ahmed
>
>
>
> Without a dataset it is harder to know what is exactly going on
>
>
>
> How does the following look
>
>
>
> library(latticeExtra)
>
> useOuterStrips(strip      = strip.custom(factor.levels = top factor levels
>
>                                          par.strip.text = list(cex =
0.9)),
>
>                  strip.left = strip.custom(factor.levels = left factor
> levels,
>
>                                          par.strip.text = par.strip.text =
> list(cex= 0.9)),
>
>
>
> xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
>
> layout=c(5,3,1),
>
> xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
>
> ylab=expression(bold(paste("Grain yield (Mg  ",ha^-1,")"))),
>
>         ylim=c(10,20),
>
>         scales = list(alternating=F,
>
>                              y=list(at=c(10,12,14,16,18,20,22)),font=2,
> cex=0.7, col="black"),aspect = 1,
>
>   panel = function(x,y,subscripts,groups,...) {
>
>    #panel.grid(-1,-1)
>
>          m1 <- lm(y ~ x)
>
>          x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
>
>          pred <- predict(m1, x.seq, se.fit=T)
>
>          grid.polygon(c(x.seq$x,rev(x.seq$x)),
>
>           c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
>
>           gp=gpar(col=0, fill="light grey", alpha = 1), default.units =
> "native")
>
>    llines(x.seq$x, pred$fit, lty = 1)
>
>          panel.xyplot(x,y)
>
>          print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
>
>          ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[ 2,c(1,4)
> ],3),sep=""),
>
>                                  cex=0.65, adj=0)
>
>         ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[
1,c(1,4)
> ],3),sep=""),
>
>                                  cex=0.65, adj=0)
>
>                                  print(summary(m1))
>
>     }
>
>    )
>
> ) ## useOuterStrips
>
>
>
> You need to fill the strips in the order of the panels so you will have to
> put a string of factor names that correspond to the strips on each of the
> panels. It makes it easier with useOuterStrips
>
>
>
> Have a look at strip.default
>
>
>
> xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
>
> layout=c(5,3,1),
>
> xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
>
>
>
> ylab=expression(bold(paste("Grain yield (Mg  ",ha^-1,")"))),
>
>         ylim=c(10,20),
>
>        strip=strip.custom(interaction(S,Loc),style=2,  # Guess but
something
> along these lines
>
>         par.strip.text = list(cex = 0.9)),
>
>         scales =
> list(alternating=F,y=list(at=c(10,12,14,16,18,20,22)),font=2, cex=0.7,
> col="black"),aspect = 1,
>
>   panel = function(x,y,subscripts,groups,...) {
>
>    #panel.grid(-1,-1)
>
>          m1 <- lm(y ~ x)
>
>          x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
>
>          pred <- predict(m1, x.seq, se.fit=T)
>
>          grid.polygon(c(x.seq$x,rev(x.seq$x)),
>
>           c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
>
>           gp=gpar(col=0, fill="light grey", alpha = 1), default.units =
> "native")
>
>    llines(x.seq$x, pred$fit, lty = 1)
>
>          panel.xyplot(x,y)
>
>          print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
>
>          ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[ 2,c(1,4)
> ],3),sep=""),
>
>                                  cex=0.65, adj=0)
>
>         ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[
1,c(1,4)
> ],3),sep=""),
>
>                                  cex=0.65, adj=0)
>
>                                  print(summary(m1))
>
>     }
>
>    )
>
>
>
> Regards
>
>
>
> Duncan
>
>
>
> Duncan Mackay
>
> Department of Agronomy and Soil Science
>
> University of New England
>
> Armidale NSW 2351
>
> Email: home: mackay at northnet.com.au
>
>
>
>
>
> From: Ahmed Attia [mailto:ahmedatia80 at gmail.com]
> Sent: Saturday, 17 January 2015 06:52
> To: r-help
> Cc: Duncan Mackay
> Subject: Specifying strip names in xyplot (strip.custom)
>
>
>
> Hi R Users,
>
>
>
> I would like to specify strip names in xyplot. I have 5 sources (S) and 3
> locations (Loc). How I can force R to specifiy the strip names for each
> source and location.
>
>
>
>
>
> Below is my code;
>
>
>
> S*Loc represents source by location. I do not know how to specify which
> panel or factors.levels. In the code below, factor.levels represent three
> Loc
>
>
>
>
>
> Thanks for your help.
>
>
>
>
>
> Ahmed
>
> trellis.device(col=F)
>
> xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
>
> layout=c(5,3,1),
>
> xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
>
>
>
> ylab=expression(bold(paste("Grain yield (Mg  ",ha^-1,")"))),
>
>         ylim=c(10,20),
>
>
>
strip=strip.custom(which.panel=3,factor.levels=c("Pierce","HAL","Concord"),s
trip.names=c(TRUE),strip.levels=c(TRUE),style=2,
>
>         par.strip.text = list(cex = 0.9)),
>
>         scales =
> list(alternating=F,y=list(at=c(10,12,14,16,18,20,22)),font=2, cex=0.7,
> col="black"),aspect = 1,
>
>   panel = function(x,y,subscripts,groups,...) {
>
>    #panel.grid(-1,-1)
>
>          m1 <- lm(y ~ x)
>
>          x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
>
>          pred <- predict(m1, x.seq, se.fit=T)
>
>          grid.polygon(c(x.seq$x,rev(x.seq$x)),
>
>           c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
>
>           gp=gpar(col=0, fill="light grey", alpha = 1), default.units =
> "native")
>
>    llines(x.seq$x, pred$fit, lty = 1)
>
>          panel.xyplot(x,y)
>
>          print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
>
>          ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[ 2,c(1,4)
> ],3),sep=""),
>
>                                  cex=0.65, adj=0)
>
>         ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[
1,c(1,4)
> ],3),sep=""),
>
>                                  cex=0.65, adj=0)
>
>                                  print(summary(m1))
>
>     }
>
>    )
>
>
>
>
>
>
>
>
> Ahmed Attia, Ph.D.
>
> Agronomist & Soil Scientist
>
> Post-Doc Research Associate
> Texas A&M AgriLife Research-Vernon
> ahmed.attia at ag.tamu.edu
> Cell phone: 001-979-248-5215
>
>



More information about the R-help mailing list